python - 内容长度被剥离

标签 python web-services web.py

我使用的是 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 :

  1. [snip]
  1. 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.
  1. 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/

相关文章:

python - Unicode解码错误: 'utf8' codec can't decode byte 0xcb in position 5: invalid continuation byte

python - IF 语句导致 webpy 出现内部服务器错误

python - 如何同时运行 PHP 和 Web.py

Python 3 统一码解码错误 : 'ascii' codec can't decode byte 0xe2 in position 0: ordinal not in range(128)

python - 根据特定值显示行

java - 如果混合全局构建和配置文件构建,cxf-codegen-plugin 会生成错误代码

javascript - 如何向浏览器检查器隐藏我的网络服务链接?

python - 删除除具有最小日期的数组之外的所有嵌套 JSON 数组

python - 当我们在 while 循环中给出两个比较运算符时,python 如何理解?

java - 从消息响应中删除 soap header