cherrypy两台服务器不同的端口

2024-06-26 10:33:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我想在不同的端口和不同的应用程序上运行两个cherry py服务器。在

我可以同时运行它们,但是如何在应用程序和服务器之间连接呢? 我想能去

http://127.0.0.1:3141/
得到server1
以及

http://127.0.0.1:3145/
得到server2

cherrypy文档的多个服务器示例不足以让我理解如何做到这一点。
这里是Multiple servers/ports

我的代码

import cherrypy

class App1(object):
    @cherrypy.expose
    def index(self):
         return ('server1')

class App2(object):
    @cherrypy.expose
    def index(self):
         return ('server2')


cherrypy.server.unsubscribe()

server1 = cherrypy._cpserver.Server()
server1.socket_port=3141
server1._socket_host="127.0.0.1"
server1.thread_pool=2
server1.subscribe()

server2 = cherrypy._cpserver.Server()
server2.socket_port=3145
server2._socket_host="127.0.0.1"
server2.thread_pool=2
server2.subscribe()

cherrypy.engine.start()
cherrypy.engine.block()

Tags: self服务器应用程序httpindexreturnobjectdef
2条回答

如果你的应用程序需要(或者可以)从不同的路径(例如http://127.0.0.1:3141/app1和{})提供服务,只需为每个应用程序使用^{}。如果两个应用程序都必须从根路径提供服务,请查看VirtualHost以获得一个想法。在

我想你需要两个不同的目录。在每个目录/应用程序中,您都为应用程序放置一个配置文件。 例如,我有这样的东西:

[global]
server.socket_host = "ip_address"
server.socket_port = 8080
server.thread_pool = 10


[/]
tools.staticdir.root = "/path/to/your/app"
tools.encode.on = True
tools.decode.on = True

看看here

相关问题 更多 >