app.py 文件源码

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

项目:opendxl-maxmind-service-python 作者: opendxl 项目源码 文件源码
def _update_database(self):
        """
        Determines if a database update is needed and if so downloads a new one from MaxMind
        """
        if not self._download_database:
            logger.warning("_update_database called while in pre-specified database"
                           " mode... returning without updates...")
            return

        logger.info("Checking for MaxMind database updates...")
        try:
            if not self._is_update_needed():
                logger.info("No database updates to retrieve.")
                return

            logger.info("Retrieving MaxMind Database...")
            response = requests.get(self.MAXMIND_FREE_DB_URL)
            response.raise_for_status()
            logger.info('Retrieved MaxMind database.')

            data = gzip.GzipFile(fileobj=StringIO.StringIO(response.content))

            # Write the database to a temporary file
            fd, file_path = tempfile.mkstemp()
            with os.fdopen(fd, 'wb') as temp_file:
                temp_file.write(data.read())

            self._swap_database(file_path)
            logger.info("MaxMind database updated.")
        except:
            logger.exception("Failed to update MaxMind database.")
        finally:
            # Schedule this function to run again in the configured update interval
            self._update_thread = Timer(self._update_interval * 60 * 60, self._update_database)
            self._update_thread.daemon = True
            self._update_thread.start()
评论列表
文章目录


问题


面经


文章

微信
公众号

扫码关注公众号