6

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 
jmunsch
  • 22,771
  • 11
  • 93
  • 114
RR1
  • 333
  • 1
  • 6
  • 13
  • 1
    possible duplicate of ["getaddrinfo failed", what does that mean?](http://stackoverflow.com/questions/7334199/getaddrinfo-failed-what-does-that-mean) – Bruno Gelb Apr 04 '14 at 00:54
  • You should add a bit more context to this question. What is the code for, and which platform your are trying it on, at least. – Totoro Apr 04 '14 at 01:09

1 Answers1

6

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

Prints:

$ ./foo.py
<class 'socket.gaierror'>
[Errno 8] nodename nor servname provided, or not known

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'

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

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

So GAI Error = Get Address Info Error.

andrewdotn
  • 32,721
  • 10
  • 101
  • 130
  • 1
    This doesn't really answer the question. The code attempts to access a server which cannot be reached, perhaps because you are not connected to the public Internet, or perhaps because it no longer exists. A snippet out of the implicated `__init__.py` file should help clarify this. – tripleee Apr 04 '14 at 04:01
  • how can we get addr info ? –  Mar 11 '21 at 11:17