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

M.-A. Lemburg mal at lemburg.com
Fri May 7 04:50:42 EDT 1999


Barry A. Warsaw wrote:
> 
> >>>>> "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()?

No, I think it's ok with changed docs; thanks for the
gethostbyname() suggestion...

BTW, I noticed that on Win95 the gethostbyname() API can hang for
minutes if there is no DNS host around (and no suitable entry in the
hosts file). Is there a work-around for this, so that I can provide
a timeout value for that lookup ?

[On Linux, this does not happen... gethostbyname() simply returns
with an exception.]

-- 
Marc-Andre Lemburg
______________________________________________________________________
Y2000:                                            Y2000: 238 days left
Business:                                      http://www.lemburg.com/
Python Pages:                 http://starship.python.net/crew/lemburg/





More information about the Python-list mailing list