美文网首页
Python_下载大文件校验

Python_下载大文件校验

作者: 古佛青灯度流年 | 来源:发表于2020-02-28 11:55 被阅读0次

    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
    

    相关文章

      网友评论

          本文标题:Python_下载大文件校验

          本文链接:https://www.haomeiwen.com/subject/aiikhhtx.html