]> khamidou — GitHub mirrors - flanker.git/commitdiff
If DNS can't resolve the first time, try again. Also cleaned up v0.3.2
authorRussell Jones <rjones@mailgunhq.com>
Mon, 18 Nov 2013 19:33:15 +0000 (11:33 -0800)
committerRussell Jones <rjones@mailgunhq.com>
Mon, 18 Nov 2013 19:33:15 +0000 (11:33 -0800)
code for mail exchanger lookup.

flanker/addresslib/address.py
flanker/addresslib/validate.py
setup.py
tests/addresslib/metrics_test.py
tests/addresslib/plugins/aol_test.py
tests/addresslib/plugins/gmail_test.py
tests/addresslib/plugins/google_test.py
tests/addresslib/plugins/hotmail_test.py
tests/addresslib/plugins/icloud_test.py
tests/addresslib/plugins/yahoo_test.py
tests/addresslib/validator_test.py

index 913757f80b939131a1acd50cc31531d10f10e92f..6f30e43f40e68275dc15ee5874a14a9eae34bfea 100644 (file)
@@ -192,12 +192,12 @@ def validate_address(addr_spec, metrics=False):
         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
@@ -249,13 +249,13 @@ def validate_list(addr_list, as_tuple=False, metrics=False):
     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
 
index 1b30081628198497975b793a45adfd9aef719646..da74d1844ed00c41c88e5d26491e6945d6aec5cc 100644 (file)
@@ -117,13 +117,65 @@ def mail_exchanger_lookup(domain, metrics=False):
     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
@@ -131,41 +183,15 @@ def mail_exchanger_lookup(domain, metrics=False):
     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):
index 5a202695187b6cd0a622370a57921c4918e02424..07e5f6978a215caf5c136f56867ef5e76f28c5c8 100644 (file)
--- a/setup.py
+++ b/setup.py
@@ -3,7 +3,7 @@
 from setuptools import setup, find_packages
 
 setup(name='flanker',
-      version="0.3.1",
+      version="0.3.2",
       description="Mailgun Parsing Tools",
       long_description='',
       classifiers=[],
index 00e5db29678957977dbd1af5ee34ad93c55bb2d7..92ddd2af34c7d50f89f840a1f282d7d0b765142c 100644 (file)
@@ -12,14 +12,14 @@ def mock_exchanger_lookup(arg, metrics=False):
     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():
index ce3e0e64b7390ccdb879e441cabe8cc29aeb96f2..634602dc37ccfa69b8fa2a09e7d72e59004fa559 100644 (file)
@@ -18,7 +18,7 @@ SAMPLE_MX = 'sample.mx.aol.com'
 @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():
index dd80057a29fbf1916d479e05d6491acc017e18fd..2aa0d92b6d6c8a73aa4111b2c85b73dcd85b3fdd 100644 (file)
@@ -19,7 +19,7 @@ ATOM_STR = string.ascii_letters + string.digits + '!#$%&\'*+-/=?^_`{|}~'
 @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():
index bb8184b28146e762c1a2640b470fd99ed8d88dac..260c937c7f2dcfcb526b81f56836992b81970f64 100644 (file)
@@ -19,7 +19,7 @@ ATOM_STR = string.ascii_letters + string.digits + '!#$%&\'*+-/=?^_`{|}~'
 @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():
index 2edf7266cc667ec38ffaa1bb187c376ecdc4d0fd..f12dd7d360260536a0f8048fed13744253d48895 100644 (file)
@@ -18,7 +18,7 @@ SAMPLE_MX = 'mx0.hotmail.com'
 @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():
index bc7e8868c1939517694fd1311660d777a82d4f1b..8588d02b934a995bf42e2c77278994c1fbe08171 100644 (file)
@@ -18,7 +18,7 @@ SAMPLE_MX = 'sample.icloud.com.akadns.net'
 @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():
index 6e1fd2c57982f4d9b6d6df809db795aa52a66abd..3582c5fec2a21ba54ee5f9f062544d75483911f4 100644 (file)
@@ -18,7 +18,7 @@ SAMPLE_MX = 'sample.yahoodns.net'
 @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():
index e44dd4ab60740cd924ea094721fa7930fa923bb6..9e64dc7980714e2ec3bdc72ee6819641bfb76e06 100644 (file)
@@ -61,15 +61,14 @@ def mock_exchanger_lookup(arg, metrics=False):
     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'):
@@ -225,14 +224,14 @@ def test_mx_lookup_metrics():
     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: