mirror of
https://github.com/aljazceru/validate_email.git
synced 2025-12-19 23:14:20 +01:00
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:
@@ -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()
|
||||||
|
|||||||
Reference in New Issue
Block a user