You can hook up middleware, actually. HTTPServer request handlers are just callable objects (function, methods, or objects that implement __call__). You can write your own handler that passes on requests to your Application
(示例)
my_app = tornado.web.Application(...)
def middleware(request):
# do whatever transformation you want here
my_app(request)
if __name__ == '__main__':
http_server = tornado.httpserver.HTTPServer(middleware)
# ...
但请注意:
Since Tornado request handling can be asynchronous, you can't modify the response in your middleware, but you can at least work with the request.
你至少有三种选择:
使用
RequestHandler.prepare()
作为kamushin said。正如this tornado issue comment中所说:
(示例)
但请注意:
使用装饰工。有关如何实现它的详细信息,请参见this exaemple。
尝试一下tornado-middleware或tmiddleware。小心点,因为他们3岁了,可能不工作。但是你总是可以看看他们的代码并从中学习。
您可以使用RequestHandler.prepare()
相关问题 更多 >
编程相关推荐