Python_下载大文件校验

1.下载文件

requests.adapters.DEFAULT_RETRIES = 5
response = requests.get(url, stream=True)
status = response.status_code
if status == 200:
    total_size = int(response.headers['Content-Length'])
    with open('xxx', 'wb') as of:
        for chunk in response.iter_content(chunk_size=102400): # 块状下载,节省内存空间
            if chunk:
                of.write(chunk)

2.校验下载完成文件与原始文件大小。

with open('xxx', 'r') as f:
    if isinstance(f, file):
        length = os.fstat(f.fileno()).st_size
        
if total_size == length:
    True
else:
    False

推荐阅读更多精彩内容