By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,095 Members | 1,586 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,095 IT Pros & Developers. It's quick & easy.

Host resolution problems with socket module

P: n/a
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
je****@uiuc.edu | www.nearestneighbor.net

Jul 21 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.