gpt4 book ai didi

Python 套接字.gaierror : [Errno 11001] getaddrinfo failed

转载 作者:行者123 更新时间:2023-12-05 04:47:08 25 4
gpt4 key购买 nike

试图在 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 = 'email1@gmail.com'
sender_pass = 'password'
receiver_address = 'email2@gmail.com'
#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 导入不可用

谢谢!

最佳答案

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

关于Python 套接字.gaierror : [Errno 11001] getaddrinfo failed,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68576559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com