6.1.6 - Better GeoIP error handling

This commit is contained in:
Sean Whalen
2019-02-16 13:50:39 -05:00
parent 39d71968f1
commit de8b4f936c
4 changed files with 22 additions and 9 deletions

View File

@@ -1,3 +1,8 @@
6.1.6
-----
- Better GeoIP error handling
6.1.5
-----

View File

@@ -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:'

View File

@@ -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)

View File

@@ -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"