Raise utils.DownloadError exception when a GeoIP database or Public Suffix List (PSL) download fails (closes issue #73)
This commit is contained in:
Sean Whalen
2019-05-19 13:21:06 -04:00
parent ffdeb8cfd3
commit 15b444141f
4 changed files with 24 additions and 13 deletions
+6
View File
@@ -1,3 +1,9 @@
6.4.1
-----
- Raise `utils.DownloadError` exception when a GeoIP database or Public
Suffix List (PSL) download fails (closes issue #73)
6.4.0
-----
+1 -1
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.4.0"
__version__ = "6.4.1"
logging.basicConfig(
format='%(levelname)8s:%(filename)s:%(lineno)d:'
+16 -11
View File
@@ -51,6 +51,10 @@ class EmailParserError(RuntimeError):
"""Raised when an error parsing the email occurs"""
class DownloadError(RuntimeError):
"""Rasied when an error occurs when downloading a file"""
def decode_base64(data):
"""
Decodes a base64 string, with padding being optional
@@ -91,9 +95,13 @@ def get_base_domain(domain, use_fresh_psl=False):
url = "https://publicsuffix.org/list/public_suffix_list.dat"
# Use a browser-like user agent string to bypass some proxy blocks
headers = {"User-Agent": USER_AGENT}
fresh_psl = requests.get(url, headers=headers).text
with open(psl_path, "w", encoding="utf-8") as fresh_psl_file:
fresh_psl_file.write(fresh_psl)
try:
fresh_psl = requests.get(url, headers=headers).text
with open(psl_path, "w", encoding="utf-8") as fresh_psl_file:
fresh_psl_file.write(fresh_psl)
except Exception as error:
raise DownloadError(
"Failed to download an updated PSL {0}".format(error))
if use_fresh_psl:
if not os.path.exists(psl_path):
@@ -102,11 +110,8 @@ def get_base_domain(domain, use_fresh_psl=False):
psl_age = datetime.now() - datetime.fromtimestamp(
os.stat(psl_path).st_mtime)
if psl_age > timedelta(hours=24):
try:
download_psl()
except Exception as error:
logger.warning(
"Failed to download an updated PSL {0}".format(error))
download_psl()
with open(psl_path, encoding="utf-8") as psl_file:
psl = publicsuffix2.PublicSuffixList(psl_file)
@@ -289,9 +294,9 @@ def get_ip_address_country(ip_address, parallel=False):
shutil.move(tar_path, location)
shutil.rmtree(tar_dir)
except Exception as e:
logger.warning("Error downloading {0}: {1}".format(url,
e.__str__()))
raise DownloadError("Error downloading {0}: {1}".format(
url,
e.__str__()))
system_paths = [
"GeoLite2-Country.mmdb",
"/usr/local/share/GeoIP/GeoLite2-Country.mmdb",
+1 -1
View File
@@ -14,7 +14,7 @@ from setuptools import setup
from codecs import open
from os import path
__version__ = "6.4.0"
__version__ = "6.4.1"
description = "A Python package and CLI for parsing aggregate and " \
"forensic DMARC reports"