【发布时间】:2018-07-12 08:06:32
【问题描述】:
我正在尝试使用 python 创建一个代理,该代理还可以读取请求和响应的内容,我正在使用它来执行此操作:https://github.com/inaz2/proxy2/blob/python3/proxy2.py
但由于某种原因,我无法解压缩任何 gzip 压缩的有效载荷。到目前为止我所尝试的:
@staticmethod
def decode_content_body(data, encoding):
print(encoding) # -> 'gzip'
if not data:
return None
if encoding == 'identity':
text = data
elif encoding in ('gzip', 'x-gzip'):
try:
data = data.encoded('latin_1')
# data = str(data) # no luck
# data = data.encoded() # no luck
compressed_stream = StringIO(data)
gzipper = gzip.GzipFile(fileobj=compressed_stream)
text = gzipper.read() # -> TypeError: can't concat str to bytes
except:
# data has to be bytes like object, says zlib
# text = zlib.decompress(data.encode()) # -> zlib.error: Error -3 while decompressing data: incorrect header check
text = zlib.decompress(data.encode(), -zlib.MAX_WBITS) # -> zlib.error: Error -3 while decompressing data: invalid block type
elif encoding == 'deflate':
try:
text = zlib.decompress(data)
except zlib.error:
text = zlib.decompress(data, -zlib.MAX_WBITS)
else:
raise Exception("Unknown Content-Encoding: {}".format(encoding))
return text
数据不是人类可读的格式,因此它显然是用某些东西压缩的。代理正在使用 HTTPS 的网站。
【问题讨论】:
标签: python python-3.x https decode zlib