diff --git a/CHANGELOG.md b/CHANGELOG.md index 424ec67..6384d37 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,3 +1,8 @@ +6.1.6 +----- + +- Better GeoIP error handling + 6.1.5 ----- diff --git a/parsedmarc/__init__.py b/parsedmarc/__init__.py index 619e39d..21018fb 100644 --- a/parsedmarc/__init__.py +++ b/parsedmarc/__init__.py @@ -38,7 +38,7 @@ from parsedmarc.utils import is_outlook_msg, convert_outlook_msg from parsedmarc.utils import timestamp_to_human, human_timestamp_to_datetime from parsedmarc.utils import parse_email -__version__ = "6.1.5" +__version__ = "6.1.6" logging.basicConfig( format='%(levelname)8s:%(filename)s:%(lineno)d:' diff --git a/parsedmarc/utils.py b/parsedmarc/utils.py index 40fd836..0648cf1 100644 --- a/parsedmarc/utils.py +++ b/parsedmarc/utils.py @@ -272,13 +272,19 @@ def get_ip_address_country(ip_address): # Use a browser-like user agent string to bypass some proxy blocks headers = {"User-Agent": USER_AGENT} original_filename = "GeoLite2-Country.mmdb" - tar_bytes = requests.get(url, headers=headers).content - tar_file = tarfile.open(fileobj=BytesIO(tar_bytes), mode="r:gz") - tar_dir = tar_file.getnames()[0] - tar_path = "{0}/{1}".format(tar_dir, original_filename) - tar_file.extract(tar_path) - shutil.move(tar_path, location) - shutil.rmtree(tar_dir) + try: + response = requests.get(url, headers=headers) + response.raise_for_status() + tar_bytes = response.content + tar_file = tarfile.open(fileobj=BytesIO(tar_bytes), mode="r:gz") + tar_dir = tar_file.getnames()[0] + tar_path = "{0}/{1}".format(tar_dir, original_filename) + tar_file.extract(tar_path) + shutil.move(tar_path, location) + shutil.rmtree(tar_dir) + except Exception as e: + logging.debug("Error downloading {0}: {1}".format(url, + e.__str__())) system_paths = ["/usr/local/share/GeoIP/GeoLite2-Country.mmdb", "/usr/share/GeoIP/GeoLite2-Country.mmdb" @@ -294,6 +300,8 @@ def get_ip_address_country(ip_address): db_path = os.path.join(tempdir, "GeoLite2-Country.mmdb") if not os.path.exists(db_path): download_country_database(db_path) + if not os.path.exists(db_path): + return None else: db_age = datetime.now() - datetime.fromtimestamp( os.stat(db_path).st_mtime) diff --git a/setup.py b/setup.py index 5bd11f3..bd4aeaa 100644 --- a/setup.py +++ b/setup.py @@ -14,7 +14,7 @@ from setuptools import setup from codecs import open from os import path -__version__ = "6.1.5" +__version__ = "6.1.6" description = "A Python package and CLI for parsing aggregate and " \ "forensic DMARC reports"