smtplib connect()调用总是超时

时间:2019-07-20 03:41:48

标签: python-3.x email-validation smtplib

我正在尝试使用这种方法(请参阅下文)来验证电子邮件地址,这种方法在无数不同的地方以一种或另一种形式被引用和推荐。问题是当我进入“ server.connect(mxRecord)”部分时,它停顿并超时了100%。

我尝试过:

  • 至少30个不同的域名,以确保这不是特定于某个域的一些网络问题
  • 调整超时限制
  • 明确指定要连接的端口(465&587)
  • 运行“ smtplib.SMTP_SSL()”而不是“ smtplib.SMTP()”

我的设置:

  • 家庭wifi
  • 没有代理人
import dns.resolver
import smtplib
import socket

addressToVerify = 'rickymartin@yahoo.com'
domainToVerify = 'yahoo.com'

records = dns.resolver.query(domainToVerify, 'MX')
mxRecord = records[0].exchange
mxRecord = str(mxRecord)
print(mxRecord)

server = smtplib.SMTP(timeout=20)
server.set_debuglevel(0)
server.connect(mxRecord)
server.helo(server.local_hostname)
server.mail('me@domain.com')
code, message = server.rcpt(str(addressToVerify))
server.quit()

if code == 250:
    print('Valid')
else:
    print('Invalid')

这是我收到的错误:


Traceback (most recent call last):
  File "email-validator.py", line 17, in <module>
    server.connect(mxRecord)
  File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 336, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/smtplib.py", line 307, in _get_socket
    self.source_address)
  File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 727, in create_connection
    raise err
  File "/usr/local/Cellar/python/3.7.1/Frameworks/Python.framework/Versions/3.7/lib/python3.7/socket.py", line 716, in create_connection
    sock.connect(sa)
socket.timeout: timed out

0 个答案:

没有答案