Exemplo n.º 1
0
def download_large_file(url, path, md5sum=None):
    """ Downloads large file from :url: to :path:

    :url: unicode
        url of file to download
    :path: unicode
        location where to download the file
    :md5sum: unicode [optional]
        md5 checksum
    """
    # download file and compute md5
    md5 = hashlib.md5()
    with closing(urllib2.urlopen(url)) as request:
        # read file size from headers
        file_size = int(request.info().getheaders('Content-Length')[0])
        print 'file size:', human_readable_size(file_size)
        progressbar = ProgressBar(file_size)
        with open(path, 'wb') as output_file:
            while True:
                chunk = request.read(CHUNK)
                if not chunk:
                    break
                output_file.write(chunk)
                md5.update(chunk)
                progressbar.add(len(chunk))
        progressbar.finish()
    # check MD5 checksum
    if md5sum:
        if md5.hexdigest() == md5sum:
            print 'MD5 checksum: OK'
        else:
            print 'Wrong MD5 checksum! Try download the file again.'