python - Tornado: Are there Request Filters? -
i looking @ tornado app , wondering how perform use case. need intercept every request before dispatching url handler , possibly return redirect. there way using tornado?
i wondering if tornado has concept of servlet filter. looks maybe input modifier decode_argument
does this? seems bit of hack filter request using method, haven't found else in documentation.
you have al least 3 options:
use
requesthandler.prepare()
kamushin said.as said in this tornado issue comment:
you can hook middleware, actually. httpserver request handlers callable objects (function, methods, or objects implement
__call__
). can write own handler passes on requestsapplication
(example)
my_app = tornado.web.application(...) def middleware(request): # whatever transformation want here my_app(request) if __name__ == '__main__': http_server = tornado.httpserver.httpserver(middleware) # ...
but notice that:
since tornado request handling can asynchronous, can't modify response in middleware, can @ least work request.
use decorator. see this exaemple details on how implementing it.
give try tornado-middleware or tmiddleware. carefull since they're 3 years old , may not work. can take @ code , learn it.
Comments
Post a Comment