gpt4 book ai didi

python - (python) [Errno 11001] getaddrinfo 失败

转载 作者:太空宇宙 更新时间:2023-11-04 01:18:23 25 4
gpt4 key购买 nike

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

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 那是什么类型的异常:

#!/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 ,它说,

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

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

关于python - (python) [Errno 11001] getaddrinfo 失败,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22851609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com