我需要使用请求调用来下载tar gz文件,我发现requests.get会自动解压缩文件,我尝试使用给定的解决方案
here
但当我试图用tar解压它时,它说它不是gzip格式。
我尝试了以下方法:
response = requests.get(url,auth=(user, key),stream=True)
if response.status_code == 200:
with open(target_path, 'wb') as f:
f.write(response.raw)
if response.status_code == 200:
with open(target_path, 'wb') as f:
f.write(response.raw)
raw = response.raw
with open(target_path, 'wb') as out_file:
while True:
chunk = raw.read(1024, decode_content=True)
if not chunk:
break
out_file.write(chunk)
以上所有内容在解压缩时都会引发错误:
$ tar -xvzf /tmp/file.tar.gz -C /
gzip: stdin: not in gzip format
tar: Child returned status 1
tar: Error is not recoverable: exiting now
注意:不能使用
urllib.open
因为我需要身份验证等,所以我必须使用请求库