From 892f86427878ac2a0f9f81b08cd74fbeb7ed604e Mon Sep 17 00:00:00 2001 From: Christophe de Vienne Date: Tue, 8 Apr 2014 12:06:31 +0200 Subject: [PATCH] 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. --- validate_email.py | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/validate_email.py b/validate_email.py index 508b8d8..ba909fa 100644 --- a/validate_email.py +++ b/validate_email.py @@ -86,6 +86,7 @@ ADDR_SPEC = LOCAL_PART + r'@' + DOMAIN # see 3.4.1 VALID_ADDRESS_REGEXP = '^' + ADDR_SPEC + '$' MX_DNS_CACHE = {} +MX_CHECK_CACHE = {} def get_mx_ip(hostname): @@ -129,8 +130,11 @@ def validate_email(email, check_mx=False, verify=False, debug=False, smtp_timeou return False for mx in mx_hosts: try: + if not verify and mx[1] in MX_CHECK_CACHE: + return MX_CHECK_CACHE[mx[1]] smtp = smtplib.SMTP(timeout=smtp_timeout) smtp.connect(mx[1]) + MX_CHECK_CACHE[mx[1]] = True if not verify: try: smtp.quit()