Bug in socket.connect_ex() implementation or docs ?!

Barry A. Warsaw bwarsaw at cnri.reston.va.us
Thu May 6 16:32:07 EDT 1999


>>>>> "M" == M  <mal at lemburg.com> writes:

    MAL> I just noticed that the .connect_ex() does raise
    MAL> socket.errors, even though the docs say, it does not. The
    MAL> exception I get is:
	
    MAL> 	socket.error: host not found

    MAL> I think either the docs or the implementation should be
    MAL> changed...  guess which one is simpler ;-)

Dang, you're right.  This is happening because three calls up --
gethostbyname() inside getsockaddrarg() inside getsockaddrarg() --
is failing and setting the exception.

It's not clear to me what the code fix would be.  Should connect_ex()
clear the exception and return h_error when it finds that
getsockaddrarg() failes?  What if there is no h_error?  And how is
this fact best propagated back down to PySocketSock_connect_ex()?

Note that there's an easy workaround: call gethostbyname first
yourself and catch any exceptions there.  If valid, pass the resulting
IP address to connect_ex(), e.g.:

try:
    ip = socket.gethostbyname('somemachine')
    conn = s.connect_ex(ip, PORT)
except socket.error:
    pass

Does this defeat too much of the purpose of connect_ex()?

-Barry




More information about the Python-list mailing list