Tornado未正确返回HTTP状态消息

时间:2015-10-30 01:01:41

标签: http python-3.x tornado haproxy

我想向我的网络应用的用户显示错误。在处理请求时,我可能会像这样提出HTTPError

raise tornado.web.HTTPError(403, reason="You're not authorised")

在我的开发环境中运行时,会产生如下响应状态:

403 You're not authorised

但是当我在制作中运行时,我得到:

403 Forbidden

更改serve_traceback and debug options没有帮助:在回复正文中返回回溯,但状态消息仍然只是“禁止”。

为什么它会在生产中返回错误信息?

龙卷风4.1

1 个答案:

答案 0 :(得分:3)

你是否在某种可能替换此字符串的代理后面运行?

一般情况下,您不应该将reason字符串用于您关心的任何内容。代理通常会替换它,浏览器不会对它做任何事情,而在HTTP / 2中它完全删除了。 Tornado允许自定义此字符串的主要原因是,如果您要使用非标准错误代码,我们必须在其中放置某些内容,因此如果需要reason参数,则您的状态代码不在httplib

在Tornado中,当您关心的只是状态时,将使用raise HTTPError(status)。如果要发送消息,请改用此模式:

self.set_status(403)
self.write("You're not authorized")
# or self.render("error.html", reason="You're not authorized"))
return
# or raise tornado.web.Finish() if you're too deep in the stack to return