(python)[Errno 11001]getaddrinfo失败

2024-05-29 01:55:09 发布

您现在位置:Python中文网/ 问答频道 /正文

有人能帮我找出这个错误吗?

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 

Tags: nameinbuildbylinecodecountrywin
1条回答
网友
1楼 · 发布于 2024-05-29 01:55:09

那么,让我们问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 ^{} documentation,上面说

This exception is raised for address-related errors, for getaddrinfo() and getnameinfo()

所以GAI Error=Get Address Info Error。

相关问题 更多 >

    热门问题