Host resolution problems with socket module

Jacob Lee jelee2 at uiuc.edu
Mon Jul 4 01:50:39 EDT 2005


I'm getting a rather bizarre error while using the socket module. If I
start out disconnected from the net and then connect while the program or
interpreter session is open, I do not always gain the ability to resolve
or connect to hosts by name. Here is the problematic scenario:

<Unplug network cable>

>>> from socket import *
>>> gethostbyname('google.com') # Fails as expected
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 1, in connect
socket.gaierror: (-3, 'Temporary failure in name resolution')

<Plug network cable back in, verify that I am really connected, etc.>

>>> gethostbyname('google.com') # Should succeed
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "<string>", line 1, in connect
socket.gaierror: (-3, 'Temporary failure in name resolution')

After some testing, I believe that this behavior is dependent on whether
the first call to gethostbyname() succeeded or not. If the first call
succeeded, subsequent calls will either resolve the host or raise an
exception depending on the status of the internet connection. If the first
call failed (i.e. raised the above exception), subsequent ones will always
raise the same exception no matter if the connection has been restored.

I should also add that getaddrinfo() and .connect() on socket objects
both fail in the same way as gethostbyname(). Deleting the socket module
and then importing it again does not help, nor does calling
reload(socket). I've been testing this on Linux with both python 2.3 and
python 2.4. As far as I can tell, the only variable that affects this
behavior is the status of the connection at the time gethostbyname() is
first called.

Is there anything else I can do to fix this problem? Is it maybe a python
bug, or is it representative of some lower-level subtlety (e.g. in the C
libraries) that I'm unaware of? Has anyone else run into this before?

-- 
Jacob Lee
jelee2 at uiuc.edu | www.nearestneighbor.net




More information about the Python-list mailing list