【问题标题】:Python CherryPy server, what does cherrypy.Application do?Python CherryPy 服务器,cherrypy.Application 是做什么的?
【发布时间】:2015-11-16 22:19:47
【问题描述】:

我这里有一个 Python 程序。 它使用 CherryPy 创建服务器。

# coding:utf-8

import os.path
import cherrypy

from app import application

def main():

try:                                   
    currentDir_s = os.path.dirname(os.path.abspath(__file__))
except:
    currentDir_s = os.path.dirname(os.path.abspath(sys.executable))
cherrypy.Application.currentDir_s = currentDir_s

configFileName_s = 'server.conf'
if os.path.exists(configFileName_s) == False:
    configFileName_s = None

cherrypy.engine.autoreload.unsubscribe()
cherrypy.engine.timeout_monitor.unsubscribe()

cherrypy.quickstart(application.Application_cl(), config=configFileName_s)

if __name__ == '__main__':
    main()

并在“server.conf”中配置服务器:

[global]
tools.log_headers.on: True
tools.sessions.on:    False  
tools.encode.on:      True
tools.encode.encoding:"utf-8"

server.socket_port:   8080 
server.socket_timeout:60     

server.thread_pool:  10      
server.environment:  "production"
log.screen:          True    

[/]
tools.staticdir.root: cherrypy.Application.currentDir_s
tools.staticdir.on = True
tools.staticdir.dir = '.'

有一件事,我不明白,这一行(python代码中的第13行):

cherrypy.Application.currentDir_s = currentDir_s

我在互联网上搜索过这个,但我找不到任何东西。 “cherrypy.Application”有什么作用?为什么我必须做这个作业(cherrypy.Application.currentDir_s = currentDir_s)?

【问题讨论】:

    标签: python cherrypy


    【解决方案1】:

    我搜索了cherrypy 源代码,这就是我找到的。 在_cptree.py 模块中,您将找到Application 类。在此之下,有一个 Tree 类,它具有 mount 方法,我们用于绑定应用程序(例如 cherrypy.tree.mount(Root(), "/", config=config)

    def mount(self, root, script_name="", config=None):
        ...
    

    当您查看此方法内部时,您将看到以下代码;

    def mount(self, root, script_name="", config=None):
        ...
        if isinstance(root, Application):
            app = root
            if script_name != "" and script_name != app.script_name:
                raise ValueError(
                    "Cannot specify a different script name and pass an "
                    "Application instance to cherrypy.mount")
            script_name = app.script_name
        else:
            app = Application(root, script_name)
    
            # If mounted at "", add favicon.ico
            if (script_name == "" and root is not None
                    and not hasattr(root, "favicon_ico")):
                favicon = os.path.join(os.getcwd(), os.path.dirname(__file__),
                                       "favicon.ico")
                root.favicon_ico = tools.staticfile.handler(favicon)
    
        if config:
            app.merge(config)
    
        self.apps[script_name] = app
    

    因此,代码表明您传递给mount 方法的每个对象(应用程序)要么是Application 实例,要么包装在Application 实例中。那为什么会这样呢?当您检查Tree 类上方的Application 类时,您将看到如下__call__ 方法;

    def __call__(self, environ, start_response):
        return self.wsgiapp(environ, start_response)
    

    是的,你现在看到了,它是wsgi 接口。 因此,Application 是您的 Cherrypy 应用程序的 wsgi 包装器。 当你检查cherrypy的源代码时,你可能会学到很多东西。希望这个回答对你有所帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-06-26
      • 2021-12-02
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-06-08
      • 1970-01-01
      相关资源
      最近更新 更多