- try:
- logging.info(" Connecting to '{0}' ...".format(url))
-
- response = get(url, stream=True)
- code = response.status_code
-
- logging.info(" {0}: {1}".format(code, responses[code]))
-
- if code != codes["OK"]:
- return False
-
- logging.info(" Downloading the file '{0}' to '{1}' ...".
- format(url, file_name))
-
- file_handle = open(file_name, "wb")
- for chunk in response.iter_content(chunk_size=CHUNK_SIZE):
- if chunk:
- file_handle.write(chunk)
- file_handle.close()
- success = True
- except ConnectionError as err:
- logging.error("Not possible to connect to '{0}'.".format(url))
- logging.debug(str(err))
- except HTTPError as err:
- logging.error("Invalid HTTP response from '{0}'.".format(url))
- logging.debug(str(err))
- except TooManyRedirects as err:
- logging.error("Request exceeded the configured number "
- "of maximum re-directions.")
- logging.debug(str(err))
- except Timeout as err:
- logging.error("Request timed out.")
- logging.debug(str(err))
- except RequestException as err:
- logging.error("Unexpected HTTP request exception.")
- logging.debug(str(err))
- except (IOError, ValueError, KeyError) as err:
- logging.error("Download failed.")
- logging.debug(str(err))
-
- logging.info(" Download finished.")
- return success
+ while repeat:
+ repeat -= 1
+ session = None
+ try:
+ logging.info(f" Connecting to {url} ...")
+ session = requests_retry_session()
+ response = session.get(url, stream=True, verify=verify)
+ code = response.status_code
+ logging.info(f" {code}: {responses[code]}")
+
+ if code != codes[u"OK"]:
+ if session:
+ session.close()
+ url = url.replace(u"_info", u"")
+ logging.info(f" Connecting to {url} ...")
+ session = requests_retry_session()
+ response = session.get(url, stream=True, verify=verify)
+ code = response.status_code
+ logging.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"")
+ logging.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)