(python) [Errno 11001] getaddrinfo 失败
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22851609/
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
(python) [Errno 11001] getaddrinfo failed
提问by RR1
Can someone help me on how I can catch this error?
有人可以帮助我了解如何捕获此错误吗?
import pygeoip
gi = pygeoip.GeoIP('GeoIP.dat')
print gi.country_code_by_name('specificdownload.com')
Traceback (most recent call last):
File "<module1>", line 14, in <module>
File "build\bdist.win-amd64\egg\pygeoip\__init__.py", line 447, in country_code_by_name
addr = self._gethostbyname(hostname)
File "build\bdist.win-amd64\egg\pygeoip\__init__.py", line 392, in _gethostbyname
return socket.gethostbyname(hostname)
gaierror: [Errno 11001] getaddrinfo failed
采纳答案by andrewdotn
Well, let's ask Python what type of exception that is:
好吧,让我们问 Python 是什么类型的异常:
#!/usr/bin/env python2.7
import pygeoip
gi = pygeoip.GeoIP('GeoIP.dat')
try:
print gi.country_code_by_name('specificdownload.com')
except Exception, e:
print type(e)
print e
Prints:
印刷:
$ ./foo.py
<class 'socket.gaierror'>
[Errno 8] nodename nor servname provided, or not known
So we need to catch socket.gaierror
, like so:
所以我们需要 catch socket.gaierror
,像这样:
#!/usr/bin/env python2.7
import pygeoip
import socket
gi = pygeoip.GeoIP('GeoIP.dat')
try:
print gi.country_code_by_name('specificdownload.com')
except socket.gaierror:
print 'ignoring failed address lookup'
Though there's still the question of, what the heck is gaierror
? Google turns up the socket.gaierror
documentation, which says,
虽然还有一个问题,到底是gaierror
什么?谷歌轮番上涨的socket.gaierror
文档,它说,
This exception is raised for address-related errors, for
getaddrinfo()
andgetnameinfo()
这个异常是针对地址相关的错误引发的,对于
getaddrinfo()
和getnameinfo()
So GAI Error = Get Address Info Error.
所以 GAI 错误 = 获取地址信息错误。