我使用的是 webpy 0.34,python 2.6.6。我也在使用 mimerender。我试图在我的 http 响应中包含内容长度,但由于某种原因, header 被删除。我说删除是因为我可以很好地创建自定义 header ,并且我可以在客户端上看到这些 header 。但是当我尝试设置内容长度时, header 永远不会到达客户端。我尝试过将 header 包含在 web.created 对象中(如图所示),并且我也尝试过使用
web.header('Content-Length', len(data))
我做错了什么和/或不理解这段代码的工作原理?
render_json = lambda **args: json.JSONEncoder().encode(args)
class MyHandler:
@mimerender(
default = 'json',
json = render_json,
)
def POST(self):
data = "abcd"
raise web.created(data, headers={'Content-Length': len(data)})
最佳答案
如果数据以分块形式发送(Transfer-Encoding: chunked
),则必须省略 Content-Length
header ,as per RFC 2616 :
- [snip]
- If a Transfer-Encoding header field (section 14.41) is present and has any value other than "identity", then the transfer-length is defined by use of the "chunked" transfer-coding (section 3.6), unless the message is terminated by closing the connection.
- If a Content-Length header field (section 14.13) is present, its decimal value in OCTETs represents both the entity-length and the transfer-length. The Content-Length header field MUST NOT be sent if these two lengths are different (i.e., if a Transfer-Encoding header field is present). If a message is received with both a Transfer-Encoding header field and a Content-Length header field, the latter MUST be ignored.
关于python - 内容长度被剥离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5553151/