def _retreive_file_lines(self, filename_format, station, year):
string = BytesIO()
if self.ftp is None:
self.ftp = self._get_ftp_connection()
for station_id in self._get_potential_station_ids(station):
filename = filename_format.format(station=station_id, year=year)
try:
self.ftp.retrbinary('RETR {}'.format(filename), string.write)
except (IOError, ftplib.error_perm) as e1:
logger.warn(
"Failed FTP RETR for station {}: {}."
" Not attempting reconnect."
.format(station_id, e1)
)
except (ftplib.error_temp, EOFError) as e2:
# Bad connection. attempt to reconnect.
logger.warn(
"Failed FTP RETR for station {}: {}."
" Attempting reconnect."
.format(station_id, e2)
)
self.ftp.close()
self.ftp = self._get_ftp_connection()
try:
self.ftp.retrbinary('RETR {}'.format(filename),
string.write)
except (IOError, ftplib.error_perm) as e3:
logger.warn(
"Failed FTP RETR for station {}: {}."
" Trying another station id."
.format(station_id, e3)
)
else:
break
else:
break
logger.info(
'Successfully retrieved ftp://ftp.ncdc.noaa.gov{}'
.format(filename)
)
string.seek(0)
f = gzip.GzipFile(fileobj=string)
lines = f.readlines()
string.close()
return lines
评论列表
文章目录