【问题标题】:(python) [Errno 11001] getaddrinfo failed(python) [Errno 11001] getaddrinfo 失败
【发布时间】:2014-05-16 02:33:34
【问题描述】:

有人可以帮我解决这个错误吗?

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 

【问题讨论】:

标签: python getaddrinfo


【解决方案1】:

好吧,让我们问一下 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 错误 = 获取地址信息错误。

【讨论】:

  • 这并不能真正回答问题。该代码尝试访问无法访问的服务器,可能是因为您没有连接到公共 Internet,或者可能是因为它不再存在。从涉及的 __init__.py 文件中提取出来的 sn-p 应该有助于澄清这一点。
  • 如何获取地址信息?
猜你喜欢
  • 2018-02-12
  • 2021-10-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-09-26
  • 1970-01-01
相关资源
最近更新 更多