Python 3 resuma a file download

zljubisic at zljubisic at
Wed Jul 1 21:59:21 CEST 2015

New version with chunks:

import os
import urllib.request

def Download(rfile, lfile):

    retval = False

    if os.path.isfile(lfile):
        lsize = os.stat(lfile).st_size
        lsize = 0

    req = urllib.request.Request(rfile)
    req.add_header('Range', "bytes={}-".format(lsize))

    response = urllib.request.urlopen(req)

    with open(lfile, 'ab') as out_file:
        while True:
                chunk =
                if not chunk: break
            except ConnectionResetError as e:
                print('Exception ConnectionResetError {0}'.format(os.stat(lfile).st_size))

    if response.headers.headers['Content-Length'] == os.stat(lfile).st_size:
        retval = True

    return retval

Download('', 'c:\\Users\\zoran\\hrt\\sync\\otv296.mp4')

More information about the Python-list mailing list