+ def requests_retry_session(retries=3,
+ backoff_factor=0.3,
+ status_forcelist=(500, 502, 504)):
+ """
+
+ :param retries: Total number of retries to allow.
+ :param backoff_factor: A backoff factor to apply between attempts after
+ the second try.
+ :param status_forcelist: A set of integer HTTP status codes that are
+ forced to retry.
+ :type retries: int
+ :type backoff_factor: float
+ :type status_forcelist: iterable
+ :returns: Session object.
+ :rtype: requests.Session
+ """
+
+ retry = Retry(
+ total=retries,
+ read=retries,
+ connect=retries,
+ backoff_factor=backoff_factor,
+ status_forcelist=status_forcelist,
+ )
+ adapter = HTTPAdapter(max_retries=retry)
+ session = requests.Session()
+ session.mount(u"http://", adapter)
+ session.mount(u"https://", adapter)
+ return session
+
+ success = False
+ session = None
+ try:
+ log.append((u"INFO", f" Connecting to {url} ..."))
+ session = requests_retry_session()
+ response = session.get(url, stream=True)
+ code = response.status_code
+ log.append((u"INFO", f" {code}: {responses[code]}"))
+
+ if code != codes[u"OK"]:
+ if session:
+ session.close()
+ url = url.replace(u"_info", u"")
+ log.append((u"INFO", f" Connecting to {url} ..."))
+ session = requests_retry_session()
+ response = session.get(url, stream=True)
+ code = response.status_code
+ log.append((u"INFO", f" {code}: {responses[code]}"))
+ if code != codes[u"OK"]:
+ return False, file_name
+ file_name = file_name.replace(u"_info", u"")
+
+ dst_file_name = file_name.replace(u".gz", u"")
+ log.append(
+ (u"INFO", f" Downloading the file {url} to {dst_file_name} ...")
+ )
+ with open(dst_file_name, u"wb") as file_handle:
+ for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
+ if chunk:
+ file_handle.write(chunk)
+
+ if arch and u".gz" in file_name:
+ if session:
+ session.close()
+ log.append(
+ (u"INFO", f" Downloading the file {url} to {file_name} ...")
+ )
+ session = requests_retry_session()
+ response = session.get(url, stream=True)
+ if response.status_code == codes[u"OK"]:
+ with open(file_name, u"wb") as file_handle:
+ file_handle.write(response.raw.read())