【问题标题】:cherrypy not working as expected using multiple servers and multiple ports使用多个服务器和多个端口时,cherrypy 无法按预期工作
【发布时间】:2011-11-03 17:39:39
【问题描述】:

我想使用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 上为私有类提供服务!

我使用的是cherrypy 3.1.2

我正在尝试遵循这些示例:123 它们彼此非常不同、不完整或似乎有错误。

【问题讨论】:

  • 为什么不使用两个独立的服务器?无论如何,从安全的角度来看,这可能是必要的,因为有权访问私有内容的服务器可以作为不同的 uid 运行。
  • @Celada 这就是我认为我正在做的事情。 server1 和 server2 是单独的服务器实例。你还有别的想法吗?
  • 我认为使用 vhost 调度程序可以帮助您。 request.dispatch = cherrypy.dispatch.VirtualHost(**{...})
  • 我的意思(并且应该说)是两个完全独立的 Python 脚本,每个服务器一个。不过 Anders Waldenborg 的建议看起来很有趣,您可以先尝试一下。

标签: python cherrypy


【解决方案1】:

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

即使安装多个根对象是可能的(据我所知,目前不可能),您的代码中没有任何地方可以将server1 明确分配给TestPublic 并将server2 分配给@ 987654328@。您只需致电 subscribe() 并希望 CherryPy 能够解决这个问题 - 这不会奏效。

正如 Anders Waldenborg 所说,查看 VirtualHost 调度程序:

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

但为此,您必须将两个根对象合二为一。

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

【讨论】:

    猜你喜欢
    • 2020-05-08
    • 2019-01-25
    • 1970-01-01
    • 1970-01-01
    • 2015-02-20
    • 2015-09-01
    • 1970-01-01
    • 2021-09-27
    • 2016-05-17
    相关资源
    最近更新 更多