为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

MMMHUHU 2022-09-13 19:22:32
让我们创建一个应用程序服务器和管理服务器。假设这一点,并包含我们要公开的应用程序和管理逻辑。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)但是服务器本身的参数在哪里 - 其中包括正在侦听的端口?
查看完整描述

1 回答

?
Smart猫小萌

TA贡献1911条经验 获得超7个赞

对于樱桃皮来说,这不是一个得到良好支持的案例。


应用程序选择(基本上是/path ->应用程序的映射)是在请求调度之前完成的,并且...长话短说,您可以在主应用程序下使用和映射您的子应用程序(这将根据主机名(端口可以是其一部分)进行路由)。对于多个端口的监听,可以做到,但同样这是一个非常自定义的安排。cherrypy.treecherrypy.dispatch.VirtualHost


我希望这个例子能说明一种可能的方式来实现这样的壮举:


import cherrypy


from cherrypy import dispatch

from cherrypy._cpserver import Server



class 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)并在不同的进程上为每个路径提供服务。这可能是一种替代方案,只有当您真的想避免在设置中出现任何额外的服务器或进程时。


查看完整回答
反对 回复 2022-09-13
  • 1 回答
  • 0 关注
  • 76 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号