cherrypy在使用多个服务器和多个端口时无法按预期工作

2024-06-26 10:34:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我想使用cherrypy在两个不同的端口上提供两个不同的类。一个类是私有的,它只希望在被防火墙阻止的端口上提供服务,并且希望使用需要凭据的服务器。另一个我想公开。在

这是我的代码:

import cherrypy
from cherrypy import _cpserver
from cherrypy import _cpwsgi_server

class TestPublic:
    @cherrypy.expose
    def index(self):
        return 'welcome!'

class TestPrivate:
    @cherrypy.expose
    def index(self):
        return 'secret!'        

if __name__ == '__main__':   
    users = {'admin':'password'}
    config1 = {'/':{
        'server.thread_pool' : 50,
        'server.environment' : 'production',
        }}
    config2 = {'/admin':{
        'server.thread_pool' : 50,
        'tools.digest_auth.on': True,
        'tools.digest_auth.realm': 'Some site',
        'tools.digest_auth.users': users,
        }}    

    cherrypy.server.unsubscribe()
    cherrypy.tree.mount(TestPublic(), script_name ='/', config=config1)
    cherrypy.tree.mount(TestPrivate(), script_name ='/admin', config=config2)        
    server1 = _cpserver.Server()
    server2 = _cpserver.Server()
    server1.bind_addr = ('0.0.0.0', 8080)
    server2.bind_addr = ('0.0.0.0', 8888)
    adapter1 = _cpserver.ServerAdapter(cherrypy.engine, server1)
    adapter2 = _cpserver.ServerAdapter(cherrypy.engine, server2)
    adapter1.subscribe()
    adapter2.subscribe()

    cherrypy.engine.start()
    cherrypy.engine.block()

它最终在8888上不提供任何服务,它在端口8080上为私有类服务!在

我用的是Cherrypy3.1.2

我试图遵循以下示例:123 它们彼此非常不同、不完整或似乎有错误。在


Tags: 端口namefromimportauthserveradmintools
1条回答
网友
1楼 · 发布于 2024-06-26 10:34:29

您的代码的问题是您为两个实例调用cherrypy.tree.mount()。但是这两个都安装在同一个CherryPy实例中,TestPrivate有效地覆盖了TestPublic。这也是为什么只有私有服务器可见的原因。在

即使挂载几个根对象是可能的(据我所知目前不是这样),在代码中也没有地方显式地将server1分配给TestPublic,并将{}显式分配给TestPrivate。你只要打电话给subscribe()并希望CherryPy能弄明白-那是行不通的。在

正如Anders Waldenborg所指出的,查看虚拟主机调度器:

http://docs.cherrypy.org/stable/refman/_cpdispatch.html#cherrypy._cpdispatch.VirtualHost

但要做到这一点,您必须将两个根对象合并为一个。在

否则,按照Celada的建议,只需启动两个单独的CherryPy过程。在

相关问题 更多 >