How to resume file download in Python? -
i using python 2.7 requests module download binary file using following code, how make code "auto-resume" download partially downloaded file.
r = requests.get(self.fileurl, stream=true, verify=false, allow_redirects=true) if r.status_code == 200: chunk_size = 8192 bytes_read = 0 open(filesave, 'wb') f: itrcount=1 chunk in r.iter_content(chunk_size): itrcount=itrcount+1 f.write(chunk) bytes_read += len(chunk) total_per = 100 * float(bytes_read)/float(long(audiosize)+long(videosize)) self.progress_updates.emit('%d\n%s' % (total_per, 'download progress : ' + self.size_human(itrcount*chunk_size) + '/' + total_size)) r.close()
i prefer use requests
module achieve if possible.
if web server supports range request can add range header request:
range: bytes=startpos-stoppos
you receive part between startpos , stoppos. if dont know stoppos use:
range: bytes=startpos-
so code 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)
Comments
Post a Comment