def download_url(source_url, target_path, print_status=False):
    in_file = urllib2.urlopen(source_url)
    total_bytes = in_file.info().getheader('Content-Length')
    if total_bytes:
        total_bytes = int(total_bytes)

    num_bytes = 0
    out_file = open(target_path, 'wb')
    def status_str():
        if total_bytes:
            return 'Downloaded %s/%s (%d%%)' % (formatting.size_str(num_bytes), formatting.size_str(total_bytes), 100.0 * num_bytes / total_bytes)
        else:
            return 'Downloaded %s' % (formatting.size_str(num_bytes))
    while True:
        s = in_file.read(BUFFER_SIZE)
        if not s: break
        out_file.write(s)
        num_bytes += len(s)
        if print_status:
            print >>sys.stderr, '\r' + status_str(),
            sys.stderr.flush()
    if print_status:
        print >>sys.stderr, '\r' + status_str() + ' [done]'
Translate HTTP headers to Django header names.
dataset/ETHPy150Open codalab/codalab-cli/codalab/lib/file_util.py/download_url--crossover with--dataset/ETHPy150Open etoccalino/django-rest-framework-httpsignature/rest_framework_httpsignature/authentication.py/SignatureAuthentication.header_canonical