Python socket.gaierror: [Errno 11001] getaddrinfo 失败

问题描述 投票:0回答:4

尝试用 python 创建邮件发送器,该脚本可以在我的个人笔记本电脑上运行,但是当我在我的工作笔记本电脑上运行它时(我是最近的实习生),我认为代理妨碍了与 gmail 的连接SMTP 服务器

错误如下:

File "D:\ocm-hours-report-automation\mail-manager\src\python\mail-sender.py", line 44, in <module>
    session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 255, in __init__
    (code, msg) = self.connect(host, port)
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 341, in connect
    self.sock = self._get_socket(host, port, self.timeout)
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\smtplib.py", line 312, in _get_socket
    return socket.create_connection((host, port), timeout,
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\socket.py", line 822, in create_connection
    for res in getaddrinfo(host, port, 0, SOCK_STREAM):
  File "C:\Users\myuser\AppData\Local\Programs\Python\Python39\lib\socket.py", line 953, in getaddrinfo
    for res in _socket.getaddrinfo(host, port, family, type, proto, flags):
socket.gaierror: [Errno 11001] getaddrinfo failed

代码是:

#The mail addresses and password
sender_address = '[email protected]'
sender_pass = 'password'
receiver_address = '[email protected]'
#Setup the MIME
message = MIMEMultipart()
message['From'] = sender_address
message['To'] = receiver_address
message['Subject'] = 'A test mail sent by Python. It has an attachment.'   #The subject line
#The body and the attachments for the mail
message.attach(MIMEText(mail_content, 'plain'))
#Create SMTP session for sending the mail
session = smtplib.SMTP('smtp.gmail.com', 587) #use gmail with port
session.starttls() #enable security
session.login(sender_address, sender_pass) #login with mail_id and password
text = message.as_string()
session.sendmail(sender_address, receiver_address, text)
session.quit()
print('Mail Sent')

有什么想法我实际上可以做什么吗?尝试强制使用socks.setdefaultproxy,但它说socks导入不可用

谢谢你!

python email proxy smtp socks
4个回答
2
投票

您的怀疑是正确的。

socket.gaierror: [Errno 11001] getaddrinfo failed
表示应用程序无法解析主机的IP地址。 根据运行代码的机器的操作系统,有多种方法可以解决这个问题,例如在 /etc/hosts 中手动映射。 但是,即使 IP 地址已解析,也不意味着代理将允许与其连接。


1
投票

就我而言,我收到此错误是因为我失去了互联网连接。 我想我应该在这里分享一下,以免有人浪费时间


1
投票

菜鸟错误,但就我而言,问题出在应用程序的启动参数上。我跑了

python manage.py  runserver myappname:8001 --insecure // wrong

我通过在启动时使用当前的“localhost”地址修复了错误:

python manage.py  runserver 0.0.0.0:8001 --insecure // working

0
投票

我遇到了同样的问题,但是当我尝试使用 python manage.py runserver 时它就可以工作。但我还是不明白问题出在哪里。

© www.soinside.com 2019 - 2024. All rights reserved.