cache the mx from the cache since it's very unlikely to change and we

can spare many DNS requests
This commit is contained in:
Andrea Crotti 2014-01-07 11:51:50 +00:00
parent 46746dc237
commit a6a10744aa
1 changed files with 10 additions and 1 deletions

View File

@ -85,6 +85,15 @@ ADDR_SPEC = LOCAL_PART + r'@' + DOMAIN # see 3.4.1
# 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
@ -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()