def _DownloadFile(self, url, max_retries=5, show_progress=None):
"""Downloads a file from and saves it to the specified location.
Args:
url: The address of the file to be downloaded.
max_retries: The number of times to attempt to download
a file if the first attempt fails.
show_progress: Print download progress to stdout (overrides default).
Raises:
DownloadError: The downloaded file did not match the expected file
size.
"""
attempt = 0
file_stream = None
opener = urllib2.OpenerDirector()
for handler in self._GetHandlers():
opener.add_handler(handler)
urllib2.install_opener(opener)
while True:
try:
attempt += 1
file_stream = urllib2.urlopen(url)
except urllib2.HTTPError:
logging.error('File not found on remote server: %s.', url)
except urllib2.URLError as e:
logging.error('Error connecting to remote server to download file '
'"%s". The error was: %s', url, e)
if file_stream:
if file_stream.getcode() in [200]:
break
else:
raise DownloadError('Invalid return code for file %s. [%d]' %
(url, file_stream.getcode()))
if attempt < max_retries:
logging.info('Sleeping for 20 seconds and then retrying the download.')
time.sleep(20)
else:
raise DownloadError('Permanent download failure for file %s.' % url)
self._StreamToDisk(file_stream, show_progress)
评论列表
文章目录