python之http.client模块请求gzip数据 作者:马育民 • 2019-01-12 16:32 • 阅读:10208 # gzip与http http://www.malaoshi.top/show_1EF2b9fjhVrl.html # 请求gzip压缩数据 增加gzip请求头,让服务器返回gzip数据,通过gzip模块对gzip数据进行解压 ``` import gzip import http.client try: conn=http.client.HTTPConnection('www.sohu.com') conn.request("GET","/",None,{'Accept-Encoding': 'gzip, deflate, br'}) resp = conn.getresponse() print(resp.status, resp.reason,resp.version) print(resp.headers) if resp.status==200: gzipdata = resp.read() #将服务器发来的gzip数据,解压出来 data = gzip.decompress(gzipdata) contenttype=resp.getheader('Content-Type') charset=contenttype.split('charset=')[1] print( data.decode(charset)) except : conn.close() raise ``` 返回的响应头里有如下: ``` 表示分块发送 Transfer-Encoding: chunked 数据经过gzip压缩 Content-Encoding: gzip ``` 原文出处:http://www.malaoshi.top/show_1EF2b9l6M8Mn.html