我目前正在从如下所示的服务器下载.tar.gz文件:
conn = http.client.HTTPSConnection(host = host,
port = port,
cert_file = pem,
key_file = key,
context = ssl.SSLContext(ssl.PROTOCOL_TLS))
conn.request('GET', url)
rsp = conn.getresponse()
fp = r"H:\path\to\new.tar.gz"
with open(fp, 'wb') as f:
while True:
piece = rps.read(4096)
if not piece:
break
f.write(piece)但是,我担心这种方法会导致压缩问题,因为有时文件仍然是this的,而其他时候则不是。
问题:
使用gzip模块从套接字流中保存文件的适当方法是什么?
支持信息:
我做了以下工作:
conn = http.client.HTTPSConnection(host = host,
port = port,
cert_file = pem,
key_file = key,
context = ssl.SSLContext(ssl.PROTOCOL_TLS))
conn.request('GET', url)
rsp = conn.getresponse()
fp = r"H:\path\to\new.tar"
f_like_obj = io.BytesIO()
f_like_obj.write(rsp.read())
f_like_obj.seek(0)
f_decomp = gzip.GzipFile(fileobj=f_like_obj, mode='rb')
with open(fp, 'wb') as f:
f.write(f_decomp.read())但是,有时在两个不同的时间下载的同一个文件会出错:
"Not a gzipped file (b'<!')"。
发布于 2017-06-28 17:19:25
试试这个:
导入http.client导入gzip
conn = http.client.HTTPSConnection(host = host,
port = port,
cert_file = pem,
key_file = key,
context = ssl.SSLContext(ssl.PROTOCOL_TLS))
conn.request('GET', url)
rsp = conn.getresponse()
fp = r"H:\path\to\new.tar"
with gzip.GzipFile(fileobj=rsp) as decomp, open(fp, 'wb') as f:
f.write(decomp.read())https://stackoverflow.com/questions/44807353
复制相似问题