【问题标题】:cherrypy two servers different ports樱桃两台服务器不同的端口
【发布时间】:2014-01-06 09:48:35
【问题描述】:

我想在不同的端口和不同的应用程序上运行 2 个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()

【问题讨论】:

标签: python cherrypy


【解决方案1】:

如果您的应用需要(或可以)从不同的路径(例如http://127.0.0.1:3141/app1http://127.0.0.1:3145/app2)提供服务,只需为每个应用使用tree.mount()。如果必须从根路径提供这两个应用程序,请查看VirtualHost 以获取建议。

【讨论】:

  • 如何在 tree.mount 和服务器之间连接?
  • @yossi 如果全部启动时它们没有自动连接,请尝试使用 HTTPServer 或 CherryPyWSGIServer。
  • 恐怕这仍然没有说明如何将一个应用程序挂载到一台服务器,将另一个应用程序挂载到另一台服务器。
  • @DrMickeyLauer 您的应用会从不同的路径提供服务吗?是的:按照Multiple servers/ports 使用ServerAdapter。否:添加VirtualHost
【解决方案2】:

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

[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

【讨论】:

  • 是的,但我如何在应用程序和服务器之间连接?
  • 抱歉,您的意思是?您是否通过在 shell 中运行以下命令来启动应用程序:python your_script.py
  • 我的代码创建了两个服务器实例,我只需要一种将应用程序连接到它们的方法
  • 如果您的意思是将同一个应用程序连接到 2 个不同的服务器,我不知道这是否可能。如果您的意思是将 2 个不同的应用程序连接到 2 个不同的服务器,我也不知道 :-) 我对cherrypy 使用不同的方法。我为每个应用程序都有一个目录,在该目录中我有 MyApp1.py 和 MyApp1.conf,所以我只是在 shell python MyApp1.py 中启动,服务器实例开始监听端口 8080(基于此处发布的文件)给出[/] tools.staticdir.root = "/path/to/your/app" 指定的目录内容。每个目录都像这样。对不起,如果我不能帮助你更多。
【解决方案3】:

经过一段时间的调整,我无法将每个应用程序安装到特定端口。 行为是每个端口都响应每个应用程序,因此我添加了一个工具来过滤每个应用程序上的端口,如下例所示:

import cherrypy
from cherrypy._cpserver import Server


@cherrypy.tools.register('before_handler')
def port_filter():
    if cherrypy.request.app.root.request_port != cherrypy.request.local.port:
        raise cherrypy.NotFound()


class App1(object):
    def __init__(self, request_port):
        self.request_port = request_port

    @cherrypy.expose
    @cherrypy.tools.port_filter()
    def index(self):
        return "Hello world from app1"


class App2(object):
    def __init__(self, request_port):
        self.request_port = request_port

    @cherrypy.expose
    @cherrypy.tools.port_filter()
    def index(self):
        return "Hello world from app2"


class MyServer(Server):
    def __init__(self, host, port, thread_pool=10):
        Server.__init__(self)
        self.socket_port = port
        self._socket_host = host
        self.thread_pool = thread_pool
        self.subscribe()


if __name__ == '__main__':
    # Unsubscribe default server
    cherrypy.server.unsubscribe()

    # Setup server for each port
    MyServer(host='0.0.0.0', port=80)
    MyServer(host='0.0.0.0', port=8080)

    # Mount applications to specific port
    cherrypy.tree.mount(App1(80), "/app1")
    cherrypy.tree.mount(App2(8080), "/app2")

    # Start!
    cherrypy.engine.start()
    cherrypy.engine.block()

【讨论】:

    猜你喜欢
    • 2012-05-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-01-02
    • 1970-01-01
    • 1970-01-01
    • 2010-11-11
    • 1970-01-01
    相关资源
    最近更新 更多