I am currently downloading .tar.gz
files from a server like so:
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)
However I am concerned that this method is causing compression issues as the files sometimes remain gzipped and other times they don't.
Question:
What is the appropriate way using the gzip
module to save a file from a socket stream?
Supporting Information:
I've done the following:
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())
This works however sometimes the same file, downloaded at two separate times, will error:
"Not a gzipped file (b'<!')"
.