How to get socket errors from 'connect'?

David Lees DavidlNospammy at raqia.com
Thu Apr 12 13:34:24 EDT 2001


I am running Python 2.0 under Redhat 7.0 Linux and trying to keep track
of error number on failed.  The code below will catch errors as an
exception, but I would like to look at the error number.  If I use
'connect_ex' mentioned in the Python documentation at:
http://www.python.org/doc/current/lib/socket-objects.html

I always get an error not caught by the Exception, so I am unable to
have the code look at the 'error indicator'

I also do not know how to access the 'error indicator'.  I see the
'errno' module which has the correspondence between a bunch of symbolic
names and their integer values, but this is no help as far as I can
tell.

Thanks in advance.

david lees




import socket

m='''GET /barf.html HTTP/1.0
Connection: close
User-Agent: Mozilla/4.75 [en] (WinNT; U)
Accept: image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, image/png,
*/*
Accept-Encoding: gzip
Accept-Language: en
Accept-Charset: iso-8859-1,*,utf-8

'''

url='192.168.0.13'
port=80

sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
    val=sock.connect((url, port))
except Exception:
    print 'no luck connecting'
    sock.close()
    del sock
    print val
    sys.exit()

print val
sock.send(m)
data=sock.recv(3000)
print data
print len(data)

sock.close
del sock



More information about the Python-list mailing list