(python)[Errno 11001] getaddrinfo失败 [英] (python) [Errno 11001] getaddrinfo failed

查看:1913
本文介绍了(python)[Errno 11001] getaddrinfo失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人可以帮助我如何捕获此错误吗?

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 

推荐答案

好,让我们问一下Python是什么类型的异常:

Well, let’s ask Python what type of exception that is:

#!/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,就像这样:

So we need to catch socket.gaierror, like so:

#!/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到底是什么? Google出现了 socket.gaierror文档,其中说,

Though there’s still the question of, what the heck is gaierror? Google turns up the socket.gaierror documentation, which says,

对于与地址相关的错误(对于getaddrinfo()getnameinfo()

所以GAI错误=获取地址信息错误.

So GAI Error = Get Address Info Error.

这篇关于(python)[Errno 11001] getaddrinfo失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆