470,620 Members | 1,735 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,620 developers. It's quick & easy.

Host resolution problems with socket module

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
0 1772

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

21 posts views Thread by Alexander N. Spitzer | last post: by
reply views Thread by Mikke Mattsson | last post: by
7 posts views Thread by none | last post: by
9 posts views Thread by Irmen de Jong | last post: by
3 posts views Thread by abcd | last post: by
2 posts views Thread by Richard Maher | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.