我正在尝试使用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的站点。
问题可能是data
传递给你的函数。
它的类型为str
,因此您用来读取请求数据的代码已经尝试将它收到的bytes
数据解码为str
。两件事之一可能出错了:
bytes
包含压缩的数据因此:str
包含胡言乱语!
使用stomp.py
包时遇到了同样的问题!在我的例子中,解决方案是在打开Stomp连接时显式设置auto_decode=False
参数。
希望有所帮助!
PS:关于我的特定问题如何解决的更多信息:https://groups.google.com/forum/#!topic/openraildata-talk/IsO206F5US8