Fixed #17 - Added timeout option for smtp connections.

This commit is contained in:
Syrus Akbary
2014-03-20 12:41:21 +01:00
parent 18dd16bc8e
commit 8e4963f42b

View File

@@ -95,7 +95,7 @@ def get_mx_ip(hostname):
return MX_DNS_CACHE[hostname] return MX_DNS_CACHE[hostname]
def validate_email(email, check_mx=False, verify=False, debug=False): def validate_email(email, check_mx=False, verify=False, debug=False, smtp_timeout=10):
"""Indicate whether the given string is a valid email address """Indicate whether the given string is a valid email address
according to the 'addr-spec' portion of RFC 2822 (see section according to the 'addr-spec' portion of RFC 2822 (see section
3.4.1). Parts of the spec that are marked obsolete are *not* 3.4.1). Parts of the spec that are marked obsolete are *not*
@@ -121,7 +121,7 @@ def validate_email(email, check_mx=False, verify=False, debug=False):
mx_hosts = get_mx_ip(hostname) mx_hosts = get_mx_ip(hostname)
for mx in mx_hosts: for mx in mx_hosts:
try: try:
smtp = smtplib.SMTP() smtp = smtplib.SMTP(timeout=smtp_timeout)
smtp.connect(mx[1]) smtp.connect(mx[1])
if not verify: if not verify:
smtp.quit() smtp.quit()
@@ -174,7 +174,7 @@ if __name__ == "__main__":
logging.basicConfig() logging.basicConfig()
result = validate_email(email, mx, validate, debug=True) result = validate_email(email, mx, validate, debug=True, smtp_timeout=1)
if result: if result:
print "Valid!" print "Valid!"
elif result is None: elif result is None: