def download_file(url, filename, quiet=True, reporthook_kwargs=None):
"""Downloads a file with optional progress report."""
if '://' not in url:
raise ValueError("fully qualified URL required: %s" % url)
if url.partition('://')[0] not in ('https', 'http', 'ftp'):
raise ValueError("unsupported URL schema: %s" % url)
if url.startswith('ftp://'):
retrieve = _urlretrieve
else:
retrieve = _urlretrieve_requests
if quiet:
return retrieve(url, filename)
reporthook_kwargs = reporthook_kwargs or {}
if filename:
reporthook_kwargs.setdefault('desc', filename)
reporthook_kwargs.setdefault('unit', 'b')
reporthook_kwargs.setdefault('unit_scale', True)
reporthook = _ReportHook(**reporthook_kwargs)
retrieve = _urlretrieve if url.startswith('ftp://') else _urlretrieve_requests
with contextlib.closing(reporthook):
retrieve(url, filename, reporthook)
评论列表
文章目录