【问题标题】:Configuring host and port in a CherryPy Python web app with more than one class在具有多个类的 CherryPy Python Web 应用程序中配置主机和端口
【发布时间】:2013-04-11 09:55:09
【问题描述】:

我有一个简单的 Cherrypy Web 应用程序,包括两个类。初始化代码如下所示:

c = MyClass()
c.updates = AnotherClass()
app = cherrypy.tree.mount(c, '/', 'myapp.config')
c.setConfig(app.config)
c.updates.setConfig(app.config)
cherrypy.engine.start()
cherrypy.engine.block()

这两个类的 setConfig 方法只是一行代码来存储一些数据库配置:

def setConfig(self, conf):
    self.config = conf['Database']

配置文件 myapp.config 如下所示:

[global]
server.socket_host = "0.0.0.0"
server.socket_port = 80

[/]
tools.staticdir.root = com.stuff.myapp.rootDir + '/html'

[Database]
dbtable: "mydbtable"
username: "user"
password: "pass"

当我开始很多时,应用程序获取数据库配置数据,并从 /html 目录正确地提供静态文件,但它只在 8080 上的 localhost 上侦听。我在控制台上得到这个:

[11/Apr/2013:10:03:58] ENGINE Bus STARTING
[11/Apr/2013:10:03:58] ENGINE Started monitor thread 'Autoreloader'.
[11/Apr/2013:10:03:58] ENGINE Started monitor thread '_TimeoutMonitor'.
[11/Apr/2013:10:03:58] ENGINE Serving on 127.0.0.1:8080
[11/Apr/2013:10:03:58] ENGINE Bus STARTED

我肯定做错了什么。就好像配置的全局部分没有得到应用。我该如何解决?

【问题讨论】:

    标签: python cherrypy


    【解决方案1】:

    我想我想出了如何解决它。我添加了这一行:

    cherrypy.config.update('myapp.config')
    

    在说的那一行之后

    app = cherrypy.tree.mount(c, '/', 'myapp.config')
    

    我认为我的类获取数据库配置的原因是我通过 setConfig() 调用手动传递了它。这只会传递 application 配置,而不是 global 配置。 mount() 调用显然不会像我认为的那样将配置数据传播到它挂载的对象。

    此外,update() 调用必须在 mount() 调用之后,否则会引发异常。

    我不确定这是否是组织此代码的最佳方式。这目前可行,但总是欢迎更好的想法。

    【讨论】:

      猜你喜欢
      • 2014-12-20
      • 2022-12-12
      • 2019-06-18
      • 2019-08-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多