seagrid.download.http#

The one HTTP(S) downloader used by every seagrid data source.

fetch() streams a URL to a local file. It retries transient server errors, writes to <file>.partial and renames on success (so an interrupted download never looks complete), optionally verifies a checksum, and raises DownloadError with the cause attached. Authenticated services pass their own requests.Session (see seagrid.download.earthdata.session()).

Examples

>>> from seagrid.download import http
>>> http.fetch("https://example.org/data/file.nc", "/tmp/data/")
PosixPath('/tmp/data/file.nc')

Module Attributes

CHUNK_SIZE

Bytes read per chunk while streaming.

RETRY_STATUS

HTTP status codes that are retried.

Functions

default_session()

Return the shared unauthenticated session (created on first use).

extract_zip(path[, dest_dir, keep])

Extract a ZIP archive.

fetch(url, dest, *[, session, timeout, ...])

Download url to a local file.

file_checksum(path[, algorithm])

Return the hex digest of a file.

list_links(url[, pattern, session, timeout])

Return the links of an HTML directory listing.

new_session([retries, backoff, pool])

Return a new requests.Session that retries transient errors.

remote_exists(url, *[, session, timeout])

Return True if url exists on the server.

uncompress(path, *[, keep])

Decompress a .gz, .bz2 or .Z file next to itself.

seagrid.download.http.default_session()[source]#

Return the shared unauthenticated session (created on first use).

Return type:

Session

seagrid.download.http.extract_zip(path, dest_dir=None, *, keep=False)[source]#

Extract a ZIP archive.

Members with absolute paths or .. components are refused.

Parameters:
  • path (str or os.PathLike) – The archive.

  • dest_dir (str or os.PathLike, optional) – Directory to extract into. Default: the archive’s directory.

  • keep (bool, optional) – Keep the archive. Default False.

Returns:

list of pathlib.Path – The extracted files.

Raises:

DownloadError – If the archive is corrupt or holds an unsafe member path.

Return type:

list[Path]

seagrid.download.http.fetch(url, dest, *, session=None, timeout=30.0, force=False, checksum=None, progress=False, description=None, verify=True, **request_kw)[source]#

Download url to a local file.

Parameters:
  • url (str) – URL to download.

  • dest (str or os.PathLike) – Target file, or an existing directory to save into under the server’s file name (from Content-Disposition or the URL path). Missing parent directories are created.

  • session (requests.Session, optional) – Session to use, e.g. an authenticated one. Default default_session().

  • timeout (float, optional) – Seconds to wait for the server to respond. Default 30.

  • force (bool, optional) – Download even if the target file exists. Default False.

  • checksum (str, optional) – Expected digest as "<algorithm>:<hex>" (e.g. "sha1:ab12..."), or a bare SHA-1 hex digest. The file is discarded on mismatch.

  • progress (bool, optional) – Show a progress bar. Default False.

  • description (str, optional) – Label of the progress bar. Default: the file name.

  • verify (bool, optional) – Verify the server’s TLS certificate. Default True.

  • **request_kw – Passed to requests.Session.get() (e.g. params, headers).

Returns:

pathlib.Path – The downloaded (or already present) file.

Raises:
  • AuthenticationError – If the server answers with a login page or HTTP 401/403.

  • DownloadError – On connection errors, other HTTP errors, an interrupted transfer or a checksum mismatch. Nothing is left at the target path.

Return type:

Path

seagrid.download.http.file_checksum(path, algorithm='sha1')[source]#

Return the hex digest of a file.

Parameters:
Returns:

str

Return type:

str

Return the links of an HTML directory listing.

Parameters:
  • url (str) – URL of the listing page.

  • pattern (str, optional) – Regular expression the link’s file name must match (re.search).

  • session (requests.Session, optional) – Session to use. Default default_session().

  • timeout (float, optional) – Seconds to wait for the server.

Returns:

list of str – Absolute URLs, in page order, without duplicates.

Raises:

DownloadError – If the page cannot be fetched.

Return type:

list[str]

seagrid.download.http.new_session(retries=5, backoff=0.5, pool=16)[source]#

Return a new requests.Session that retries transient errors.

Parameters:
  • retries (int, optional) – Retries per request for connection errors and the status codes in RETRY_STATUS. Default 5.

  • backoff (float, optional) – Exponential backoff factor in seconds. Default 0.5.

  • pool (int, optional) – Connection pool size per host. Default 16.

Returns:

requests.Session

Return type:

Session

seagrid.download.http.remote_exists(url, *, session=None, timeout=30.0)[source]#

Return True if url exists on the server.

Sends a HEAD request, falling back to a streamed GET (closed without reading the body) for servers that do not support HEAD. A login page counts as not existing.

Parameters:
  • url (str) – URL to check.

  • session (requests.Session, optional) – Session to use. Default default_session().

  • timeout (float, optional) – Seconds to wait for the server. Default 30.

Returns:

bool

Raises:

DownloadError – If the server cannot be reached, or answers with a server error (5xx), which says nothing about the file.

Return type:

bool

seagrid.download.http.uncompress(path, *, keep=False)[source]#

Decompress a .gz, .bz2 or .Z file next to itself.

Parameters:
  • path (str or os.PathLike) – Compressed file.

  • keep (bool, optional) – Keep the compressed file. Default False.

Returns:

pathlib.Path – The decompressed file (path without its suffix). A file with any other suffix is returned unchanged.

Raises:

DownloadError – If decompression fails (the partial output is removed).

Return type:

Path