urrlib2.urlopen: “Name or service not known” persists when starting script without internet connection

后端 未结 2 395
天涯浪人
天涯浪人 2020-12-10 02:58

I have this simple minimal \'working\' example below that opens a connection to google every two seconds. When I run this script when I have a working internet connection, I

相关标签:
2条回答
  • 2020-12-10 03:48

    For me, it was a proxy problem. Running the following before import urllib.request helped

    import os
    os.environ['http_proxy']=''
    response = urllib.request.urlopen('http://www.google.com')
    
    0 讨论(0)
  • 2020-12-10 04:03

    This happens because the DNS name "www.google.com" cannot be resolved. If there is no internet connection the DNS server is probably not reachable to resolve this entry.

    It seems I misread your question the first time. The behaviour you describe is, on Linux, a peculiarity of glibc. It only reads "/etc/resolv.conf" once, when loading. glibc can be forced to re-read "/etc/resolv.conf" via the res_init() function.

    One solution would be to wrap the res_init() function and call it before calling getaddrinfo() (which is indirectly used by urllib2.urlopen().

    You might try the following (still assuming you're using Linux):

    import ctypes
    libc = ctypes.cdll.LoadLibrary('libc.so.6')
    res_init = libc.__res_init
    # ...
    res_init()
    response = urllib2.urlopen('http://www.google.com')
    

    This might of course be optimized by waiting until "/etc/resolv.conf" is modified before calling res_init().

    Another solution would be to install e.g. nscd (name service cache daemon).

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