gpt4 book ai didi

python - 如何在 python 中解码 HTTP 响应中返回的 gzip 压缩数据?

转载 作者:太空狗 更新时间:2023-10-29 18:07:51 49 4
gpt4 key购买 nike

我在 python 中创建了一个客户端/服务器架构,我从客户端获取 HTTP 请求,该客户端通过我的代码请求另一个 HTTP 服务器来提供服务。

当我从第三台服务器获得响应时,我无法解码 gzip 压缩数据,我首先使用 \r\n 作为分隔符拆分响应数据,这让我得到了数据列表中的最后一项然后我尝试用

解压它
zlib.decompress(data[-1]) 

但它给我一个不正确标题的错误。我该如何解决这个问题?

代码

client_reply = ''
while 1:
chunk = server2.recv(512)
if len(chunk) :
client.send(chunk)
client_reply += chunk
else:
break
client_split = client_reply.split("\r\n")
print client_split[-1].decode('zlib')

我想读取在客户端和第二个服务器之间传输的数据。

最佳答案

在使用 zlib.decompress(string, wbits, bufsize) 时指定 wbits 示例参见“故障排除”的结尾。

疑难解答

让我们从一个 curl 命令开始,它下载一个带有未知“内容编码”的字节范围响应(注意:我们事先知道它是某种压缩的东西,也许 deflate gzip):

export URL="https://commoncrawl.s3.amazonaws.com/crawl-data/CC-MAIN-2016-18/segments/1461860106452.21/warc/CC-MAIN-20160428161506-00007-ip-10-239-7-51.ec2.internal.warc.gz"
curl -r 266472196-266527075 $URL | gzip -dc | tee hello.txt

具有以下响应 header :

HTTP/1.1 206 Partial Content
x-amz-id-2: IzdPq3DAPfitkgdXhEwzBSwkxwJRx9ICtfxnnruPCLSMvueRA8j7a05hKr++Na6s
x-amz-request-id: 14B89CED698E0954
Date: Sat, 06 Aug 2016 01:26:03 GMT
Last-Modified: Sat, 07 May 2016 08:39:18 GMT
ETag: "144a93586a13abf27cb9b82b10a87787"
Accept-Ranges: bytes
Content-Range: bytes 266472196-266527075/711047506
Content-Type: application/octet-stream
Content-Length: 54880
Server: AmazonS3

言归正传

让我们显示前 10 个字节的十六进制输出:curl -r 266472196-266472208 $URL | xxd

十六进制输出:

0000000: 1f8b 0800 0000 0000 0000 ecbd eb

我们可以看到我们正在处理的十六进制值的一些基础知识。

大致意思是它可能是一个 gzip(1f8b),使用 deflate(0800),没有修改时间(0000 0000),或任何额外的标志集 (00),使用 fat32 系统 (00)。

请引用2.3/2.3.1节:https://www.rfc-editor.org/rfc/rfc1952#section-2.3.1

所以在 python 上:

>>> import requests
>>> url = 'https://commoncrawl.s3.amazonaws.com/crawl-data/CC-MAIN-2016-18/segments/1461860106452.21/warc/CC-MAIN-20160428161506-00006-ip-10-239-7-51.ec2.internal.warc.gz'
>>> response = requests.get(url, params={"range":"bytes=257173173-257248267"})
>>> unknown_compressed_data = response.content

注意到任何类似的东西吗?

>>> unknown_compressed_data[:10]
'\x1f\x8b\x08\x00\x00\x00\x00\x00\x00\x00'

关于解压,让我们根据 (documentation) 随机尝试:

>>> import zlib

“zlib.error:准备解压缩数据时出现错误 -2:流状态不一致”:

>>> zlib.decompress(unknown_compressed_data, -31)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
zlib.error: Error -2 while preparing to decompress data: inconsistent stream state

“解压缩数据时出现错误 -3: header 检查不正确”:

>>> zlib.decompress(unknown_compressed_data)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
zlib.error: Error -3 while decompressing data: incorrect header check

“zlib.error:解压缩数据时出现错误 -3:距离太远无效”:

>>> zlib.decompress(unknown_compressed_data, 30)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
zlib.error: Error -3 while decompressing data: invalid distance too far back

可能的解决方案:

>>> zlib.decompress(unknown_compressed_data, 31)
'WARC/1.0\r\nWARC-Type: response\r\nWARC-Date: 2016-04-28T20:14:16Z\r\nWARC-Record-ID: <urn:uu

关于python - 如何在 python 中解码 HTTP 响应中返回的 gzip 压缩数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9762017/

49 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com