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
|
Bytes read per chunk while streaming. |
|
HTTP status codes that are retried. |
Functions
Return the shared unauthenticated session (created on first use). |
|
|
Extract a ZIP archive. |
|
Download url to a local file. |
|
Return the hex digest of a file. |
|
Return the links of an HTML directory listing. |
|
Return a new |
|
Return True if url exists on the server. |
|
Decompress a |
- 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 (
stroros.PathLike) – The archive.dest_dir (
stroros.PathLike, optional) – Directory to extract into. Default: the archive’s directory.keep (
bool, optional) – Keep the archive. Default False.
- Returns:
listofpathlib.Path– The extracted files.- Raises:
DownloadError – If the archive is corrupt or holds an unsafe member path.
- Return type:
- 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 (
stroros.PathLike) – Target file, or an existing directory to save into under the server’s file name (fromContent-Dispositionor the URL path). Missing parent directories are created.session (
requests.Session, optional) – Session to use, e.g. an authenticated one. Defaultdefault_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:
- seagrid.download.http.file_checksum(path, algorithm='sha1')[source]#
Return the hex digest of a file.
- Parameters:
path (
stroros.PathLike) – File to hash.algorithm (
str, optional) – Anyhashlib.new()algorithm. Default"sha1"(used by NASA OB.DAAC).
- Returns:
- Return type:
- seagrid.download.http.list_links(url, pattern=None, *, session=None, timeout=30.0)[source]#
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. Defaultdefault_session().timeout (
float, optional) – Seconds to wait for the server.
- Returns:
listofstr– Absolute URLs, in page order, without duplicates.- Raises:
DownloadError – If the page cannot be fetched.
- Return type:
- seagrid.download.http.new_session(retries=5, backoff=0.5, pool=16)[source]#
Return a new
requests.Sessionthat retries transient errors.- Parameters:
- 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
HEADrequest, falling back to a streamedGET(closed without reading the body) for servers that do not supportHEAD. A login page counts as not existing.- Parameters:
url (
str) – URL to check.session (
requests.Session, optional) – Session to use. Defaultdefault_session().timeout (
float, optional) – Seconds to wait for the server. Default 30.
- Returns:
- Raises:
DownloadError – If the server cannot be reached, or answers with a server error (5xx), which says nothing about the file.
- Return type:
- seagrid.download.http.uncompress(path, *, keep=False)[source]#
Decompress a
.gz,.bz2or.Zfile next to itself.- Parameters:
path (
stroros.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: