1

I want to download a file with Python requests lib. The things is, when i lose connection to the net then the file has to be downloaded again. The question is: how can I make program to know where he finished last and start downloading the file from this point?

I paste the code bellow

res = requests.get(link)
playfile = open(file_name, 'wb')

for chunk in res.iter_content(100000):
    playfile.write(chunk)
Artism
  • 53
  • 1
  • 7
  • How big is this file? Can it not be downloaded in one go? – QHarr Dec 02 '18 at 09:14
  • Depends, sometimes its a Video file which can be ~1Gb, sometimes a picture which weights ~0,5Mb. I use other libs to scrap the site that posts those things, the fragmen i presented is just to download a file, not to find the link. Besides that I really would like to solve this problem – Artism Dec 04 '18 at 20:32

1 Answers1

1

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)
KC.
  • 2,981
  • 2
  • 12
  • 22