【问题标题】:zlib error -3 while reading content from server response从服务器响应读取内容时出现 zlib 错误 -3
【发布时间】:2017-01-07 17:51:07
【问题描述】:

我收到来自使用 gzip 编码的服务器的响应, 但我不知道如何解码。

我尝试使用 zlib 模块,但出现错误: zlib.error: Error -3 while decompressing data: incorrect header check

这是我的要求:

r = requests.post(url=url,headers=headers,auth=auth,data=data)
print r.status_code
print r.headers
print zlib.decompress(r.content)

我得到的打印是:

200 {'Content-Length': '1232', 'Content-Encoding': 'gzip', 'Accept-Ranges': 'bytes', 'X-Powered-By': 'PHP/5.5.38', 'Vary': 'Accept-Encoding', ...

错误:

Traceback (most recent call last):
  File "C:/testing.py", line 27, in <module>
    print zlib.decompress(r.content)
zlib.error: Error -3 while decompressing data: incorrect header check

【问题讨论】:

  • requests 应该为您提供未压缩的内容,因此您不必使用 zlib。
  • 发帖Content-Type
  • 请求:'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'。回复:'Content-Type': 'text/html'
  • requests 为您解压。 content应该已经解压了,不能再解压了。
  • 好的.. 我去看看。我确实收到了文本,但其中一些似乎被压缩了。你可能是对的

标签: python gzip httpresponse zlib decoding


【解决方案1】:

尝试zlib.decompress(r.content, 31) 请求 gzip 解码而不是 zlib 解码。

【讨论】:

    猜你喜欢
    • 2021-04-29
    • 1970-01-01
    • 1970-01-01
    • 2019-11-12
    • 2020-12-18
    • 2021-01-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多