# 关键代码 stream = True 参数,以流的方式读取
# 执行代码 直接给出文件大小
url = 'url自己找吧'
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