How to resume file download in Python?

后端 未结 1 1170
暗喜
暗喜 2020-12-01 08:19

I am using python 2.7 requests module to download a binary file using the following code, how to make this code \"auto-resume\" the download from partially downloaded file.<

相关标签:
1条回答
  • 2020-12-01 08:56

    If the web server supports the range request then you can add the Range header to your request:

    Range: bytes=StartPos-StopPos
    

    You will receive the part between StartPos and StopPos. If dont know the StopPos just use:

    Range: bytes=StartPos-
    

    So your code would be:

    def resume_download(fileurl, resume_byte_pos):
        resume_header = {'Range': 'bytes=%d-' % resume_byte_pos}
        return requests.get(fileurl, headers=resume_header, stream=True,  verify=False, allow_redirects=True)
    
    0 讨论(0)
提交回复
热议问题