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)

浙公网安备 33010602011771号