Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions modflow_devtools/download.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import sys
import tarfile
import time
import timeit
import urllib.request
from os import PathLike
Expand Down Expand Up @@ -223,6 +224,7 @@ def download_and_unzip(
path: PathLike | None = None,
delete_zip=True,
retries=3,
timeout=30,
verbose=False,
) -> Path:
"""
Expand All @@ -239,6 +241,8 @@ def download_and_unzip(
Whether the zip file should be deleted after it is unzipped (default is True)
retries : int
The maximum number of retries for each request
timeout : int
Timeout in seconds for the download request
verbose : bool
Whether to show verbose output

Expand Down Expand Up @@ -271,7 +275,7 @@ def download_and_unzip(
tries += 1
try:
with (
urllib.request.urlopen(request) as url_file,
urllib.request.urlopen(request, timeout=timeout) as url_file,
file_path.open("wb") as out_file,
):
content = url_file.read()
Expand All @@ -289,10 +293,17 @@ def download_and_unzip(
print(f" file size: {sbfmt.format(bfmt.format(int(file_size)))}")

break
except urllib.error.HTTPError as err:
except (
urllib.error.HTTPError,
urllib.error.URLError,
ConnectionError,
TimeoutError,
) as err:
if tries < retries:
warn(f"URL request try {tries} failed ({err})")
time.sleep(1)
continue
raise

# write the total download time
toc = timeit.default_timer()
Expand Down
Loading