从表单:/foo/(.*)/bar/(.*)的URL中,我想提供文件,其中实际路径是从两个捕获的组中计算出来的。我的问题是StaticFileHandler的get()只接受一个path参数。是否有一种方法可以使其工作,而不必重新实现大多数StaticFileHandler的方法?
我目前的解决方法是捕获所有内容:(/foo/.*/bar/.*),但随后我必须对overriden get_absolute_path()中的类似正则表达式进行分析。
发布于 2016-08-02 15:12:38
如果不扩展StaticFileHandler,就无法做到这一点。这将是一个微小的变化:
from tornado import gen, web
class CustomStaticFileHandler(web.StaticFileHandler):
def get(self, part1, part2, include_body=True):
# mangle path
path = "dome_{}_combined_path_{}".format(part1, part2)
# back to staticfilehandler
return super().get(path, include_body)
# if you need to use coroutines on mangle use
#
# @gen.coroutine
# def get(self, part1, part2, include_body=True):
# path = yield some_db.get_path(part1, part2)
# yield super().get(path, include_body)
app = web.Application([
(r"/foo/(.*)/bar/(.*)", CustomStaticFileHandler, {"path": "/tmp"}),
])https://stackoverflow.com/questions/38703734
复制相似问题