code for mail exchanger lookup.
return None, mtimes
# lookup if this domain has a mail exchanger
- has_exchanger, exchanger, mx_metrics = \
+ exchanger, mx_metrics = \
flanker.addresslib.validate.mail_exchanger_lookup(addr_parts[-1], metrics=True)
mtimes['mx_lookup'] = mx_metrics['mx_lookup']
mtimes['dns_lookup'] = mx_metrics['dns_lookup']
mtimes['mx_conn'] = mx_metrics['mx_conn']
- if has_exchanger is False:
+ if exchanger is None:
return None, mtimes
# lookup custom local-part grammar if it exists
for paddr in parsed_addresses:
# lookup if this domain has a mail exchanger
- has_exchanger, exchanger, mx_metrics = \
+ exchanger, mx_metrics = \
flanker.addresslib.validate.mail_exchanger_lookup(paddr.hostname, metrics=True)
mtimes['mx_lookup'] += mx_metrics['mx_lookup']
mtimes['dns_lookup'] += mx_metrics['dns_lookup']
mtimes['mx_conn'] += mx_metrics['mx_conn']
- if has_exchanger is False:
+ if exchanger is None:
ulist.append(paddr.full_spec())
continue
Looks up the mail exchanger for a domain. If MX records exist they will
be returned, if not it will attempt to fallback to A records, if neither
exist None will be returned.
+ '''
+ mtimes = {'mx_lookup': 0, 'dns_lookup': 0, 'mx_conn': 0}
+ mx_cache = flanker.addresslib.mx_cache
+
+ # look in cache
+ bstart = time.time()
+ in_cache, cache_value = lookup_exchanger_in_cache(domain)
+ mtimes['mx_lookup'] = time.time() - bstart
+ if in_cache:
+ return cache_value, mtimes
+ # dns lookup on domain
+ bstart = time.time()
+ mx_hosts = lookup_domain(domain)
+ mtimes['dns_lookup'] = time.time() - bstart
+ if mx_hosts is None:
+ # try one more time
+ bstart = time.time()
+ mx_hosts = lookup_domain(domain)
+ mtimes['dns_lookup'] += time.time() - bstart
+ if mx_hosts is None:
+ mx_cache[domain] = False
+ return None, mtimes
+
+ # test connecting to the mx exchanger
+ bstart = time.time()
+ mail_exchanger = connect_to_mail_exchanger(mx_hosts)
+ mtimes['mx_conn'] = time.time() - bstart
+ if mail_exchanger is None:
+ mx_cache[domain] = False
+ return None, mtimes
+
+ # valid mx records, connected to mail exchanger, return True
+ mx_cache[domain] = mail_exchanger
+ return mail_exchanger, mtimes
+
+
+def lookup_exchanger_in_cache(domain):
+ '''
Uses a cache to store the results of the mail exchanger lookup to speed
up lookup times. The default is redis, but this can be overidden by your
own cache as long as it conforms to the same interface as that of a dict.
See the implimentation of the redis cache in the flanker.addresslib.driver
package for more details if you wish to implement your own cache.
+ '''
+ mx_cache = flanker.addresslib.mx_cache
+
+ lookup = mx_cache[domain]
+ if lookup is None:
+ return (False, None)
+ if lookup == 'False':
+ return (True, None)
+ else:
+ return (True, lookup)
+
+
+def lookup_domain(domain):
+ '''
The dnspython package is used for dns lookups. The dnspython package uses
the dns server specified by your operating system. Just like the cache,
this can be overridden by your own dns lookup method of choice as long
implimentation of the dnspython lookup in the flanker.addresslib.driver
package for more details.
'''
- mtimes = {'mx_lookup': 0, 'dns_lookup': 0, 'mx_conn': 0}
- mx_cache = flanker.addresslib.mx_cache
dns_lookup = flanker.addresslib.dns_lookup
- # if we have the mx lookup cached, return the cached result
- bstart = time.time()
- lookup = mx_cache[domain]
- mtimes['mx_lookup'] = time.time() - bstart
- if lookup is not None:
- if lookup == 'False':
- return (False, None, mtimes)
- else:
- return (True, lookup, mtimes)
-
- # check if mx records exist
fqdn = domain if domain[-1] == '.' else ''.join([domain, '.'])
- bstart = time.time()
mx_hosts = dns_lookup[fqdn]
- mtimes['dns_lookup'] = time.time() - bstart
- if len(mx_hosts) == 0:
- mx_cache[domain] = False
- return (False, None, mtimes)
- # check if we can connect to the mail exchanger
- bstart = time.time()
- mail_exchanger = connect_to_mail_exchanger(mx_hosts)
- mtimes['mx_conn'] = time.time() - bstart
- if mail_exchanger is None:
- mx_cache[domain] = False
- return (False, None, mtimes)
-
- # valid mx records, connected to mail exchanger, return True
- mx_cache[domain] = mail_exchanger
- return (True, mail_exchanger, mtimes)
+ if len(mx_hosts) == 0:
+ return None
+ return mx_hosts
def connect_to_mail_exchanger(mx_hosts):
from setuptools import setup, find_packages
setup(name='flanker',
- version="0.3.1",
+ version="0.3.2",
description="Mailgun Parsing Tools",
long_description='',
classifiers=[],
mtimes = {'mx_lookup': 10, 'dns_lookup': 20, 'mx_conn': 30}
if metrics is True:
if arg == 'ai' or arg == 'mailgun.org' or arg == 'fakecompany.mailgun.org':
- return (True, '', mtimes)
+ return ('', mtimes)
else:
- return (False, '', mtimes)
+ return (None, mtimes)
else:
if arg == 'ai' or arg == 'mailgun.org' or arg == 'fakecompany.mailgun.org':
- return (True, '')
+ return ''
else:
- return (False, '')
+ return None
def test_metrics_parse():
@nottest
def mock_exchanger_lookup(arg, metrics=False):
mtimes = {'mx_lookup': 0, 'dns_lookup': 0, 'mx_conn': 0}
- return (True, SAMPLE_MX, mtimes)
+ return (SAMPLE_MX, mtimes)
def test_exchanger_lookup():
@nottest
def mock_exchanger_lookup(arg, metrics=False):
mtimes = {'mx_lookup': 0, 'dns_lookup': 0, 'mx_conn': 0}
- return (True, SAMPLE_MX, mtimes)
+ return (SAMPLE_MX, mtimes)
def test_exchanger_lookup():
@nottest
def mock_exchanger_lookup(arg, metrics=False):
mtimes = {'mx_lookup': 0, 'dns_lookup': 0, 'mx_conn': 0}
- return (True, SAMPLE_MX, mtimes)
+ return (SAMPLE_MX, mtimes)
def test_exchanger_lookup():
@nottest
def mock_exchanger_lookup(arg, metrics=False):
mtimes = {'mx_lookup': 0, 'dns_lookup': 0, 'mx_conn': 0}
- return (True, SAMPLE_MX, mtimes)
+ return (SAMPLE_MX, mtimes)
def test_exchanger_lookup():
@nottest
def mock_exchanger_lookup(arg, metrics=False):
mtimes = {'mx_lookup': 0, 'dns_lookup': 0, 'mx_conn': 0}
- return (True, SAMPLE_MX, mtimes)
+ return (SAMPLE_MX, mtimes)
def test_exchanger_lookup():
@nottest
def mock_exchanger_lookup(arg, metrics=False):
mtimes = {'mx_lookup': 0, 'dns_lookup': 0, 'mx_conn': 0}
- return (True, SAMPLE_MX, mtimes)
+ return (SAMPLE_MX, mtimes)
def test_exchanger_lookup():
mtimes = {'mx_lookup': 10, 'dns_lookup': 20, 'mx_conn': 30}
if metrics is True:
if arg == 'ai' or arg == 'mailgun.org' or arg == 'fakecompany.mailgun.org':
- return (True, '', mtimes)
+ return ('', mtimes)
else:
- return (False, '', mtimes)
+ return (None, mtimes)
else:
if arg == 'ai' or arg == 'mailgun.org' or arg == 'fakecompany.mailgun.org':
- return (True, '')
+ return ''
else:
- return (False, '')
-
+ return None
def test_abridged_mailbox_valid_set():
for line in ABRIDGED_LOCALPART_VALID_TESTS.split('\n'):
with patch.object(validate, 'mail_exchanger_lookup') as mock_method:
mock_method.side_effect = mock_exchanger_lookup
- a, b, metrics = validate.mail_exchanger_lookup('example.com', metrics=True)
+ a, metrics = validate.mail_exchanger_lookup('example.com', metrics=True)
assert_equal(metrics['mx_lookup'], 10)
assert_equal(metrics['dns_lookup'], 20)
assert_equal(metrics['mx_conn'], 30)
# ensure values are unpacked correctly
- a, b = validate.mail_exchanger_lookup('example.com', metrics=False)
- a, b = validate.mail_exchanger_lookup('example.com', metrics=False)
+ a = validate.mail_exchanger_lookup('example.com', metrics=False)
+ a = validate.mail_exchanger_lookup('example.com', metrics=False)
def test_validate_address_metrics():
with patch.object(validate, 'mail_exchanger_lookup') as mock_method: