Python 使用http.client正确下载.gz

Python 使用http.client正确下载.gz,python,Python,我目前正在从如下服务器下载.tar.gz文件: conn = http.client.HTTPSConnection(host = host, port = port, cert_file = pem, key_file = key,

我目前正在从如下服务器下载
.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)
但是,我担心这种方法会导致压缩问题,因为文件有时保持gzip,有时则不保持gzip

问题:

使用
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())
但有时在两个不同时间下载的同一文件会出错:

“不是gzip文件(b'请尝试以下操作:

导入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())

@JoaoVitorino是与
urllib
模块一起使用的。我正在寻找
http.client
解决方案。@JoaoVitorino除此之外,还有
urllib.request.urlretrieve()
方法倾向于导致
urllib.error.HTTPError
,特别是HTTP 500内部服务器。低级
HTTP.client
允许我指定避免这种情况所需的所有详细信息。