If dnslookup return NXDOMAIN, return False

A NXDOMAIN error from dnslookup means the domain does not exists. In such a
cas, we consider the email address as being invalid.
,
This commit is contained in:
Christophe de Vienne
2014-04-08 11:59:07 +02:00
parent 25d16d8a4d
commit 26e67ff572

View File

@@ -90,7 +90,13 @@ MX_DNS_CACHE = {}
def get_mx_ip(hostname): def get_mx_ip(hostname):
if hostname not in MX_DNS_CACHE: if hostname not in MX_DNS_CACHE:
try:
MX_DNS_CACHE[hostname] = DNS.mxlookup(hostname) MX_DNS_CACHE[hostname] = DNS.mxlookup(hostname)
except ServerError, e:
if e.rcode == 3: # NXDOMAIN (Non-Existent Domain)
MX_DNS_CACHE[hostname] = None
else:
raise
return MX_DNS_CACHE[hostname] return MX_DNS_CACHE[hostname]
@@ -119,6 +125,8 @@ def validate_email(email, check_mx=False, verify=False, debug=False, smtp_timeou
DNS.DiscoverNameServers() DNS.DiscoverNameServers()
hostname = email[email.find('@') + 1:] hostname = email[email.find('@') + 1:]
mx_hosts = get_mx_ip(hostname) mx_hosts = get_mx_ip(hostname)
if mx_hosts is None:
return False
for mx in mx_hosts: for mx in mx_hosts:
try: try:
smtp = smtplib.SMTP(timeout=smtp_timeout) smtp = smtplib.SMTP(timeout=smtp_timeout)