有人可以帮我解决这个错误吗?
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
答案 0 :(得分:3)
好吧,让我们问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
打印:
$ ./foo.py
<class 'socket.gaierror'>
[Errno 8] nodename nor servname provided, or not known
所以我们需要抓住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'
虽然仍有问题,但是gaierror
到底是什么?谷歌出现了the socket.gaierror
documentation,其中说,
针对
的地址相关错误,引发了此异常getaddrinfo()
和getnameinfo()
因此GAI错误=获取地址信息错误。