(python) [Errno 11001] getaddrinfo failed

前端 未结 1 1172
遥遥无期
遥遥无期 2021-01-20 05:37

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\         


        
相关标签:
1条回答
  • 2021-01-20 05:42

    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.

    0 讨论(0)
提交回复
热议问题