Python 3 resuma a file download

zljubisic at gmail.com zljubisic at gmail.com
Thu Jul 2 22:27:24 CEST 2015


This is my final version which doesn't work. :(
Actually, it works with another file on another server, but doesn't work with mp4 files on this particular server.

I really don't know what to do?

Regards.

import os
import urllib.request

def Download(rfile, lfile):

    retval = False

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

    req = urllib.request.Request(rfile)

    rfsize = urllib.request.urlopen(req).length

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

    response = urllib.request.urlopen(req)

    with open(lfile, 'ab') as out_file:
        while True:
            try:
                chunk = response.read(64 * 1024)
                # chunk = response.read(128)
                if not chunk: break
                out_file.write(chunk)
                out_file.flush()


                lfsize = os.stat(lfile).st_size

                 print("{0:.2f}".format(lfsize / rfsize * 100))
            except ConnectionResetError as e:
                print('Exception ConnectionResetError {0}'.format(os.stat(lfile).st_size))
                response = urllib.request.urlopen(req)


    if lfsize == rfsize:
        retval = True

    return retval


while not Download('http://video.hrt.hr/2906/otv296.mp4', 'otv296.mp4'):
    print('1')


More information about the Python-list mailing list