It is possible to continue downloading from checkpoint through Range
. Actually your question is similar to How to `pause`, and `resume` download work?.
This is a example shows how it work.
import requests
def DownloadFile(url):
local_filename = url.split('/')[-1]
with requests.Session() as s:
r = s.get(url,headers={"Range": "bytes=0-999"})
with open(local_filename, 'wb') as fd:
fd.write(r.content)
r2 = s.get(url,headers={"Range": "bytes=1000-"})
with open(local_filename, 'ab') as fd:
fd.write(r2.content)
return
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/63/BBC_Radio_logo.svg/210px-BBC_Radio_logo.svg.png"
DownloadFile(url)
Now, We can build up a function which starts downlaoding a file from a checkpoint.
import requests
import os
def Continue_(url):
local_filename = url.split('/')[-1]
with requests.Session() as s:
if os.path.exists(local_filename):
position = os.stat(local_filename).st_size
else:
position = 0
r2 = s.get(url,headers={"Range": "bytes={}-".format(position)})
with open(local_filename, 'ab+') as fd:
for c in r2.iter_content():
fd.write(c)
url = "https://upload.wikimedia.org/wikipedia/commons/thumb/6/63/BBC_Radio_logo.svg/210px-BBC_Radio_logo.svg.png"
def DownloadFile(url):
local_filename = url.split('/')[-1]
with requests.Session() as s:
r = s.get(url,headers={"Range": "bytes=0-999"})
with open(local_filename, 'wb') as fd:
fd.write(r.content)
DownloadFile(url)
Continue_(url)