下载前获取文件大小
from contextlib import closing
import requests
import time
import hashlib
url = 'https://qd.myapp.com/myapp/qqteam/pcqq/PCQQ2019.exe'
re = requests.get(url, stream=True)
print(re.headers.get('Content-Length'))
def request_url(url, headers, file_path):
m = hashlib.md5()
start_time = time.time()
with open(file_path, 'wb') as code:
with closing(requests.get(url, headers=headers, timeout=3, stream=True)) as res:
file_size_str = res.headers.get('Content-Length')
if int(file_size_str) > 300000000:
return 'no', 'no', 'no'
for chunk in res.iter_content(chunk_size=10240):
if time.time() - start_time > 3:
return 'no', 'no', 'no'
start_time = time.time()
code.write(chunk)
m.update(chunk)
return m.hexdigest(), file_path, file_size_str
下载前获取文件大小
requests.get()参数详解
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/104e3ba7eec89843663471f0d219e231.png#pic_center)