Skip to content

Commit

Permalink
Merge pull request syrusakbary#12 from AndreaCrotti/dns_cache
Browse files Browse the repository at this point in the history
cache the mx from the cache since it's very unlikely to change and we can spare many DNS requests
  • Loading branch information
syrusakbary committed Jan 7, 2014
2 parents 46746dc + a6a1074 commit 18dd16b
Showing 1 changed file with 10 additions and 1 deletion.
11 changes: 10 additions & 1 deletion validate_email.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ class ServerError(Exception):
# A valid address will match exactly the 3.4.1 addr-spec.
VALID_ADDRESS_REGEXP = '^' + ADDR_SPEC + '$'

MX_DNS_CACHE = {}


def get_mx_ip(hostname):
if hostname not in MX_DNS_CACHE:
MX_DNS_CACHE[hostname] = DNS.mxlookup(hostname)

return MX_DNS_CACHE[hostname]


def validate_email(email, check_mx=False, verify=False, debug=False):
"""Indicate whether the given string is a valid email address
Expand All @@ -109,7 +118,7 @@ def validate_email(email, check_mx=False, verify=False, debug=False):
'have installed pyDNS python package')
DNS.DiscoverNameServers()
hostname = email[email.find('@') + 1:]
mx_hosts = DNS.mxlookup(hostname)
mx_hosts = get_mx_ip(hostname)
for mx in mx_hosts:
try:
smtp = smtplib.SMTP()
Expand Down

0 comments on commit 18dd16b

Please sign in to comment.