Add a cache for check_mx.

Once a mx has been checked, any further check on the same mx will immediately
return without actually connecting to the smtp server.
This commit is contained in:
Christophe de Vienne
2014-04-08 12:06:31 +02:00
parent 26e67ff572
commit 892f864278

View File

@@ -86,6 +86,7 @@ ADDR_SPEC = LOCAL_PART + r'@' + DOMAIN # see 3.4.1
VALID_ADDRESS_REGEXP = '^' + ADDR_SPEC + '$' VALID_ADDRESS_REGEXP = '^' + ADDR_SPEC + '$'
MX_DNS_CACHE = {} MX_DNS_CACHE = {}
MX_CHECK_CACHE = {}
def get_mx_ip(hostname): def get_mx_ip(hostname):
@@ -129,8 +130,11 @@ def validate_email(email, check_mx=False, verify=False, debug=False, smtp_timeou
return False return False
for mx in mx_hosts: for mx in mx_hosts:
try: try:
if not verify and mx[1] in MX_CHECK_CACHE:
return MX_CHECK_CACHE[mx[1]]
smtp = smtplib.SMTP(timeout=smtp_timeout) smtp = smtplib.SMTP(timeout=smtp_timeout)
smtp.connect(mx[1]) smtp.connect(mx[1])
MX_CHECK_CACHE[mx[1]] = True
if not verify: if not verify:
try: try:
smtp.quit() smtp.quit()