Python socket.gaierror: [Errno 11001] getaddrinfo 失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18400208/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
socket.gaierror: [Errno 11001] getaddrinfo failed
提问by user2499879
I have tried to attached a file to the mail using python. Code:
我尝试使用 python 将文件附加到邮件中。代码:
import smtplib
from email.MIMEMultipart import MIMEMultipart
from email.MIMEText import MIMEText
from smtplib import SMTPException
def send_Email():
file1="abc.txt"
message = "Test mail"
msg = MIMEMultipart()
msg.attach(MIMEText(file(file1).read()))
try:
smtpObj = smtplib.SMTP('smtp server name',port)
smtpObj.sendmail(sender, EmailId, message, msg.as_string() )
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
Bt I have get the error: socket.gaierror: [Errno 11001] getaddrinfo failed
Bt 我收到错误:socket.gaierror: [Errno 11001] getaddrinfo failed
full error message:
完整的错误信息:
File "C:\Python27\lib\smtplib.py", line 249, in __init__
(code, msg) = self.connect(host, port)
File "C:\Python27\lib\smtplib.py", line 309, in connect
self.sock = self._get_socket(host, port, self.timeout)
File "C:\Python27\lib\smtplib.py", line 284, in _get_socket
return socket.create_connection((port, host), timeout)
File "C:\Python27\lib\socket.py", line 553, in create_connection
for res in getaddrinfo(host, port, 0, SOCK_STREAM):
socket.gaierror: [Errno 11001] getaddrinfo failed
回答by Steve Barnes
The problem is that the DNS lookup for 'smtp server name' is failing - if this is your exact code then you can see why - if not and you have the valid qualified name for the SMTP server then you may have issues with the firewall/internet connection, etc., also port has to be set to a valid value to match your servers SMTP configuration, (usually port 25 butnot absolutely always).
问题是“smtp 服务器名称”的 DNS 查找失败 - 如果这是您的确切代码,那么您可以看到原因 - 如果不是,并且您拥有 SMTP 服务器的有效限定名称,那么您可能遇到防火墙问题/互联网连接等,还必须将端口设置为有效值以匹配您的服务器 SMTP 配置(通常是端口 25,但并非绝对总是如此)。
回答by Deepak Saini
I know for sure that gaierror comes up when you are working from behind proxy.
我确信当您从代理后面工作时会出现 gaierror。
回答by Ras
You need to login using your credential. Try:
您需要使用您的凭据登录。尝试:
smtpObj = smtplib.SMTP('smtp server name',port)
smtpObj .starttls()
smtpObj .login(email, password)
smtpObj.sendmail(sender, EmailId, message, msg.as_string() )
print "Successfully sent email"
回答by FunCoding
In my case was a hostproblem. Using debug mode, I spotted that in (host, port, 0, SOCK_STREAM) I got host=localand it should be host=localhost. In the run.py I defined localhostand the file hosts (c:\windows\system32\drivers\etc\hosts) was defined local. They have to be equal, otherwise you get the socket.gaieeror.
就我而言,是主机问题。使用调试模式,我发现在 (host, port, 0, SOCK_STREAM) 我得到了host=local,它应该是host=localhost。在 run.py 中,我定义了localhost并且文件 hosts (c:\windows\system32\drivers\etc\hosts) 被定义为local。它们必须相等,否则你会得到 socket.gaieeror。
回答by Gray Hat
I prefer u guys to run the file as administrator for eg open cmd as administrator then type cd C:\into ur .py file pathand then type python filename.py
我更喜欢你们以管理员身份运行文件,例如以管理员身份打开 cmd 然后输入 cd C:\into ur .py 文件路径,然后输入 python filename.py
it worked for me. good luck
它对我有用。祝你好运
回答by lmocsi
There seems to be a bug in urllib3 version 1.25.9 package. This produced "socket.gaierror: [Errno 11001] getaddrinfo failed" error for me (working from behind an authenticated proxy server). Downgrading to urllib3 version 1.25.8 solved the problem.
urllib3 版本 1.25.9 包中似乎存在一个错误。这对我产生了“socket.gaierror: [Errno 11001] getaddrinfo failed”错误(在经过身份验证的代理服务器后面工作)。降级到 urllib3 版本 1.25.8 解决了这个问题。