<p>你至少有三种选择:</p>
<ul>
<li><p>使用<code>RequestHandler.prepare()</code>作为<a href="https://stackoverflow.com/a/32195226/3219121">kamushin said</a>。</p></li>
<li><p>正如<a href="https://github.com/tornadoweb/tornado/issues/49#issuecomment-99801" rel="nofollow noreferrer">this tornado issue comment</a>中所说:</p>
<blockquote>
<p>You can hook up middleware, actually. HTTPServer request handlers are just callable objects (function, methods, or objects that implement <code>__call__</code>). You can write your own handler that passes on requests to your <code>Application</code></p>
</blockquote></li>
</ul>
<p>(示例)</p>
<pre><code>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)
# ...
</code></pre>
<p>但请注意:</p>
<blockquote>
<p>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.</p>
</blockquote>
<ul>
<li><p>使用装饰工。有关如何实现它的详细信息,请参见<a href="https://stackoverflow.com/a/16886294/3219121">this exaemple</a>。</p></li>
<li><p>尝试一下<a href="https://github.com/Homeloc/tornado-middleware" rel="nofollow noreferrer">tornado-middleware</a>或<a href="https://github.com/iamsk/tmiddleware" rel="nofollow noreferrer">tmiddleware</a>。小心点,因为他们3岁了,可能不工作。但是你总是可以看看他们的代码并从中学习。</p></li>
</ul>