【发布时间】:2012-01-01 18:13:09
【问题描述】:
我已经编写了自己的自定义调度程序,它使用正则表达式来映射路由,但是,我不能再在 /static 中托管静态文件。这是调度程序和配置:
class Dispatcher(object):
def __init__(self):
self.urls = {}
def __call__(self, path_info):
print('Dispatcher called: ' + path_info)
func = self.find_handler(path_info)
cherrypy.serving.request.handler = func
def find_handler(self, path_info):
request = cherrypy.serving.request
request.config = cherrypy.config.copy()
for url in self.urls:
args = re.findall(url, path_info)
if len(args) > 0:
# in the case that the route is just a URL, we don't want
# an extra argument in the method function
try:
args.remove(path_info)
except ValueError:
pass
controller = self.urls[url]
method = request.method.lower()
return cherrypy._cpdispatch.LateParamPageHandler(getattr(controller, method), *args)
return cherrypy.NotFound()
def connect(self, url, controller):
if not url.endswith("$"):
url += "$"
self.urls[url] = controller
还有配置:
config = {
'global': {
'server.socket_host': '0.0.0.0',
'server.socket_port': port,
},
'/static': {
'tools.staticdir.on': True,
'tools.staticdir.dir': os.path.join(os.getcwd(), 'static'),
},
'/': {
'request.dispatch': self.dispatcher,
}
}
如果我使用标准调度程序,静态文件可以正常工作,但是如果我使用自己的,它们将不再工作。在调度程序中完成调试后,静态文件将通过调度程序,即使我明确指出只有在 '/' 中才会使用调度程序。
【问题讨论】:
标签: python regex static dispatcher cherrypy