1

我之前了解到,在cherrypy中,您必须公开一个方法以使其成为视图目标,这也遍布整个文档:

  import cherrypy
  @cherrypy.expose
  def index():
    return "hello world"

但是我继承了一个cherrypy应用程序,它似乎可以在不暴露任何东西的情况下工作

这是如何运作的?是否从较新版本中删除了公开要求?

这并不容易谷歌搜索,我发现了很多关于在cherrypy上暴露和装饰器的信息,但没有关于“没有暴露的cherrypy”

这是主要的 serve.py 脚本,为了简洁起见,我从其中删除了一些部分:

# -*- coding: utf-8 -*-

import cherrypy
from root import RouteRoot


dispatcher = cherrypy.dispatch.RoutesDispatcher()
dispatcher.explicit = False
dispatcher.connect(u'system',      u'/system', RouteRoot().index)

conf = {
  '/' : {
    u'request.dispatch' : dispatcher,
    u'tools.staticdir.root' : conf_app_BASEDIR_ROOT, 
    u'log.screen' : True,
  },
  u'/my/pub' : {
    u'tools.staticdir.debug' : True,
    u'tools.staticdir.on' : True,
    u'tools.staticdir.dir' : u"pub",
  },
}
#conf = {'/' : {'request.dispatch' : dispatcher}}

cherrypy.tree.mount(None, u"/", config=conf)
import conf.ip_config as ip_config  
cherrypy.config.update({
  'server.socket_host': str(ip_config.host),
  'server.socket_port': int(ip_config.port),
})

cherrypy.quickstart(None, config=conf)

并且应用程序中的任何地方都没有 èxpose`。它如何工作?

文件根.py:

# -*- coding: utf-8 -*-

from mako.template import Template

class RouteRoot:

  def index(self):
    return "Hello world!"
4

3 回答 3

2

Because it relies on the routes dispatcher which works slightly differently. Mainly, it doesn't need the exposed attribute that the expose decorator sets because the URLs are explicitly described (as you can see with the connect(...) method). On the other hand, the default CherryPy dispatcher doesn't provide an explicit mapping between URLs and objects. When a request hits the application's engine, it must be go through the tree of applications you mounted initially. It uses the exposed attribute to determine if a method can take part in the URL mapping. This gives a chance to the developer to write methods in a controller class that can't be accessed by a URL mapping process.

于 2013-06-06T19:47:15.560 回答
0

我已阅读此页面:http ://docs.cherrypy.org/stable/concepts/dispatching.html

I am not sure but maybe the dispatching replaces the necessity for exposing.

于 2013-06-06T19:41:14.897 回答
0

It is because the app doesn't uses the default dispatcher. It is explained (in bold!) in cherrypy's doc upon dispatchers

于 2013-06-06T19:41:23.073 回答