如何使用cherrypy tree.mount指定侦听服务器实例? [英] How to specify the listening server instances using cherrypy tree.mount?

查看:36
本文介绍了如何使用cherrypy tree.mount指定侦听服务器实例?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们创建一个应用程序服务器和一个管理服务器.假设 fusionListener adminListener 包含我们要公开的应用程序和管理逻辑.

Let us create an application server and an admin server. Assume that fusionListener and adminListener contain the application and admin logic we want to expose.

  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 参数要求:

  • 将代码/业务逻辑作为第一个参数
  • 收听网址
  • 配置参数

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

Here is how that looks for the above servers:

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

但是 server 本身的参数在哪里-其中包括正在监听的 port ?

But where is the parameter for the server itself - which includes the port being listened to?

推荐答案

对于CherryPy,这不是一个很好的支持案例.

This is not a well supported case for CherryPy.

应用程序选择( cherrypy.tree 基本上是/path-> App的映射)在请求分派之前完成,总之,您可以使用 cherrypy.dispatch.VirtualHost 并将您的子应用程序映射到一个主应用程序下(该应用程序将根据主机名(该端口可以是该端口的一部分)进行路由.要监听多个端口,可以完成,但这又是一个非常自定义的安排.

The application selection (cherrypy.tree is basically a map of /path -> App) is done before the request dispatch and... long story short, you could use cherrypy.dispatch.VirtualHost and map you sub applications under a main one (that will route depending on the hostname (which the port can be part of). For the listening on multiple ports, can be done, but again this is a very custom arrangement.

我希望这个例子可以说明制作壮举的一种可能方法:

I hope this example is illustrative of a possible way to make such feat:

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

当来自 localhost:8081 时,此示例将提供 AppOne ;来自 localhost:8082 时,将提供 AppTwo

This example will serve AppOne when coming from localhost:8081 and AppTwo when coming from localhost:8082.

问题是您无法执行 cherrypy.tree.mount 的倍数,并且期望使用 VirtualHost 调度程序路由到不同的应用程序,它假定该应用程序解决方案在那时完成,仅解决了该应用程序的路径.

The problem is that you can't do multiples cherrypy.tree.mount and expect to route into the different applications using the VirtualHost dispatcher, it assumes that the application resolution is done at that point and is only resolving the path of that application.

说了这么多……我不推荐这种解决方案,它可能会变得复杂,最好在前面安装一些其他服务器(例如nginx)并在不同的进程中服务每个路径.仅当您确实确实要避免在设置中避免任何额外的服务器或进程时,这才是替代方案.

Having said all of that... I do not recommend this solution, it can get complicated and it would be better to have some other server in front (like nginx) and serve each path on different processes. This could be an alternative, only if you really really want to avoid any extra server or process in your setup.

这篇关于如何使用cherrypy tree.mount指定侦听服务器实例?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆