猿问

如何使用樱桃树挂指定监听服务器实例?

让我们创建一个应用程序服务器和管理服务器。假设这一点,并包含我们要公开的应用程序和管理逻辑。fusionListeneradminListener


  from cherrypy._cpserver import Server

  fserver = Server()

  fserver.socket_port = 10000

  fserver.subscribe()


  aserver = Server()

  aserver.socket_port = 10001

  aserver.subscribe()

然后启动它们:


cherrypy.engine.start()

cherrypy.engine.block()

参数要求:tree.mount


代码/业务逻辑作为第一个参数

侦听网址

配置参数

以下是上述服务器的外观:


  cherrypy.tree.mount(fusionListener, r"/fusion.*",fusionConf)

  cherrypy.tree.mount(adminListener, r"/admin.*",adminConf)

但是服务器本身的参数在哪里 - 其中包括正在侦听的端口?


MMMHUHU
浏览 77回答 1
1回答

Smart猫小萌

对于樱桃皮来说,这不是一个得到良好支持的案例。应用程序选择(基本上是/path ->应用程序的映射)是在请求调度之前完成的,并且...长话短说,您可以在主应用程序下使用和映射您的子应用程序(这将根据主机名(端口可以是其一部分)进行路由)。对于多个端口的监听,可以做到,但同样这是一个非常自定义的安排。cherrypy.treecherrypy.dispatch.VirtualHost我希望这个例子能说明一种可能的方式来实现这样的壮举:import cherrypyfrom cherrypy import dispatchfrom cherrypy._cpserver import Serverclass AppOne:    @cherrypy.expose    def default(self):        return "DEFAULT from app ONE!"    @cherrypy.expose    def foo(self):        return "FOO from app ONE"class AppTwo:    @cherrypy.expose    def default(self):        return "DEFAULT from app TWO!"    @cherrypy.expose    def foo(self):        return "FOO from app TWO"class Root:    def __init__(self):        self.one = AppOne()        self.two = AppTwo()def bind_two_servers(app_one_port, app_two_port):    # unsubscribe the default server    cherrypy.server.unsubscribe()    s1 = Server()    s2 = Server()    s1.socket_port = app_one_port    s2.socket_port = app_two_port    # subscribe the server to the `cherrypy.engine` bus events    s1.subscribe()    s2.subscribe()def start_server():    bind_two_servers(8081, 8082)    cherrypy.engine.signals.subscribe()    cherrypy.engine.start()    cherrypy.engine.block()config = {    '/': {        'request.dispatch': dispatch.VirtualHost(**{            'localhost:8081': '/one',            'localhost:8082': '/two',        })    }}cherrypy.tree.mount(Root(), '/', config)start_server()此示例将在来自 和 来自 时起作用。AppOnelocalhost:8081AppTwolocalhost:8082问题在于,您无法执行多个操作并期望使用调度程序路由到不同的应用程序,它假定应用程序解析在该点完成,并且仅解析该应用程序的路径。cherrypy.tree.mountVirtualHost说了这么多...我不建议这个解决方案,它可能会变得复杂,最好有一些其他服务器(如nginx)并在不同的进程上为每个路径提供服务。这可能是一种替代方案,只有当您真的想避免在设置中出现任何额外的服务器或进程时。
随时随地看视频慕课网APP

相关分类

Python
我要回答