http compres gzip

 

 

def _gzip_string(data):
try:
return gzip.compress(data)
except AttributeError:
import StringIO
buf = StringIO.StringIO()
fd = gzip.GzipFile(fileobj=buf, mode="w")
fd.write(data)
fd.close()
return buf.getvalue()

客户端

在headers中告之服务端压缩类型 

headers_compress

json_data_compress= _gzip_string(json_data_str.encode("utf-8"))

if compress:
response = requests.post(self.url, data=json_data_compress, headers=headers_compress, timeout=self.timeout)
else:
response = requests.post(self.url, json=json_data, headers=headers, timeout=self.timeout)

 

 

posted @ 2025-11-07 15:11  papering  阅读(0)  评论(0)    收藏  举报