import pathlib
import sys
import time
+import urllib.error
+import urllib.request
import zipfile
-from urllib.request import urlretrieve
def retrieve_with_retries(download_location, output_path, reporthook,
"""Download a file with exponential backoff retry and save to disk."""
for attempt in range(max_retries + 1):
try:
- resp = urlretrieve(
+ resp = urllib.request.urlretrieve(
download_location,
output_path,
reporthook=reporthook,
)
- except ConnectionError as ex:
+ except (urllib.error.URLError, ConnectionError) as ex:
if attempt == max_retries:
msg = f"Download from {download_location} failed."
raise OSError(msg) from ex
for attempt in range(max_retries + 1):
try:
resp = urllib.request.urlopen(download_location)
- except urllib.error.URLError as ex:
+ except (urllib.error.URLError, ConnectionError) as ex:
if attempt == max_retries:
msg = f"Download from {download_location} failed."
raise OSError(msg) from ex