Skip to main content

download.HTTPDownloader

Stream HTTP downloads through a reusable session.

Usage

Source

download.HTTPDownloader(
    *,
    method="GET",
    auth=None,
    trusted_hosts=None,
    timeout=DEFAULT_TIMEOUT,
    progress=True
)

Built upon requests. Inspired by pooch.HTTPDownloader. Supports downloading with GET and POST requests. Manual redirects prevent credentials from reaching untrusted hosts. Use as a context manager or call close() when finished.

Methods

Name Description
__call__() Download a URL to a local or remote directory.
__del__() Close the session, suppressing finalization errors.
__enter__() Return this downloader.
__exit__() Close the session.
__init__() Initialize the downloader.
close() Close the session.

__call__()

Download a URL to a local or remote directory.

Usage

Source

__call__(self, url: str, save_dir: str | Path, filename: str | None = None, overwrite: bool = False) -> Path
__call__(self, url: str, save_dir: UPath, filename: str | None = None, overwrite: bool = False) -> UPath
Parameters
url: str

Source URL.

save_dir: str, Path, or UPath

Destination directory. A remote directory receives the file only once it has downloaded completely to a local temporary directory.

filename: str = None

Output name, inferred from the response or URL when omitted.

overwrite: bool = False
Replace an existing target.
Returns
Path or UPath
Downloaded path.
Raises
ValueError

If the filename is unsafe.

FileExistsError

If the target exists unless overwrite is true.

DownloadError
If the response is not a downloadable file, a redirect has no host, the redirects exceed the limit, or no filename can be inferred.

__del__()

Close the session, suppressing finalization errors.

Usage

Source

__del__()

__enter__()

Return this downloader.

Usage

Source

__enter__()

__exit__()

Close the session.

Usage

Source

__exit__(*exc_info)

__init__()

Initialize the downloader.

Usage

Source

__init__(
    *,
    method="GET",
    auth=None,
    trusted_hosts=None,
    timeout=DEFAULT_TIMEOUT,
    progress=True
)
Parameters
method: ("GET", "POST") = "GET"

HTTP method used to request the file.

auth: tuple[str, str] or instance of AuthBase subclass = None

Authentication object or (user, password) pair.

trusted_hosts: str or iterable of str = None

Redirect hosts allowed to receive authentication.

timeout: float or tuple[float, float] = DEFAULT_TIMEOUT

Request timeout or (connect, read) timeouts.

progress: bool = True
Show download progress.

close()

Close the session.

Usage

Source

close()