clients.py 文件源码

python
阅读 22 收藏 0 点赞 0 评论 0

项目:eemeter 作者: openeemeter 项目源码 文件源码
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
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号