Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python cherrypy两台服务器的不同端口_Python_Cherrypy - Fatal编程技术网

Python cherrypy两台服务器的不同端口

Python cherrypy两台服务器的不同端口,python,cherrypy,Python,Cherrypy,我想在不同的端口和不同的应用程序上运行2台cherry py服务器 我成功地运行了这两个应用程序,但是如何在应用程序和服务器之间连接呢? 我想去看电影 http://127.0.0.1:3141/ 并获取server1 以及 http://127.0.0.1:3145/ 并获取server2 多服务器的cherrypy docs示例不足以让我理解如何做到这一点。 给你 我的代码 import cherrypy class App1(object): @cherrypy.expose

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

我成功地运行了这两个应用程序,但是如何在应用程序和服务器之间连接呢? 我想去看电影

http://127.0.0.1:3141/

并获取
server1

以及

http://127.0.0.1:3145/

并获取
server2

多服务器的cherrypy docs示例不足以让我理解如何做到这一点。
给你

我的代码

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()

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

[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

如果您的应用程序需要(或可以)从不同的路径(例如
http://127.0.0.1:3141/app1
http://127.0.0.1:3145/app2
),只需为每个应用程序使用。如果两个应用程序都必须从根路径提供服务,请考虑一个想法。

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

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()

是的,但我如何在应用程序和服务器之间连接?对不起,你是什么意思?你是通过在shell中运行命令启动应用程序的吗:
python your_script.py
?我的代码创建两个服务器实例我只需要一种方法将一个应用程序连接到它们中的每一个。如果你的意思是将同一个应用程序连接到两个不同的服务器,我不知道这是否可行。如果你的意思是将两个不同的应用程序连接到两个不同的服务器,我也不知道:-)我对cherrypy使用不同的方法。我为每个应用程序都有一个目录,目录中有MyApp1.py和MyApp1.conf,所以我只需在shell中启动
python MyApp1.py
,服务器实例开始监听端口8080(基于此处发布的文件),给出
[/]tools.staticdir.root=“/path/to/your/app”
指定的目录内容。每个目录都是这样的。抱歉,如果我不能帮你更多的忙。Dupe--see@jwalker我已经看到了文档,两台服务器的示例很短,我缺少所需的信息。如何在tree.mount和服务器之间连接?@yossi如果启动时它们没有自动连接,请尝试使用HTTPServer或CherryPyWSGIServer。恐怕这仍然不能清楚地说明如何将一个应用程序装载到一台服务器上,将另一个应用程序装载到另一台服务器上。@DrMickeyLauer您的应用程序将从不同的路径提供服务吗?是:只需按照使用
ServerAdapter
。否:添加。