경로를 매핑하는 정규식을 사용하는 사용자 지정 디스패처를 작성했지만 정적 파일을 /static에 더 이상 호스트 할 수 없습니다.정적 파일이있는 Cherrypy 사용자 지정 디스패처
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,
}
}
는, 정적 파일들이 정상적으로 작동 I, 그들은 더 이상 내 자신을 사용하지 그러나, 여기에 디스패처와 설정은 작업. 디스패처에서 디버깅을 수행하면 정적 파일은 디스패처를 통과합니다. '/'만 사용하면 디스패처가 사용됩니다.