如何在 Tornado 中记录 HTTP 响应? [英] How to log HTTP response in Tornado?

查看:30
本文介绍了如何在 Tornado 中记录 HTTP 响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望能够在龙卷风中记录 HTTP 请求和响应.这似乎很容易通过请求来实现:

I want to be able to log HTTP request and response in tornado. This seems to be easy to do with request:

def log_function(handler):
    info = {
        'Method' : handler.request.method,
        'Host' : handler.request.host,
        'URL' : handler.request.uri
     }

如何实现相同的响应?可以通过调用

How can the same thing be achieved for response? The response status_code can be retrieved by calling

handler.get_status()

如何获得响应正文?

推荐答案

Tornado 不保存响应;它直接发送给客户端.如果要记录响应,则必须自己保存.您可以在处理程序代码中执行此操作,也可以覆盖 write()finish() 方法以在写入时对其进行拦截.

Tornado doesn't save the response; it sends it directly to the client. If you want to log the response you'll have to save it yourself. You can either do this in your handler code or override the write() and finish() methods to intercept it as it is being written.

这篇关于如何在 Tornado 中记录 HTTP 响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆