How to know if connection is active when using telnetlib?

sp1d3rx at gmail.com sp1d3rx at gmail.com
Fri Aug 26 13:19:23 EDT 2005


Well, running this code:
----------
>>> import telnetlib
>>>  c = telnetlib.Telnet("blah")
----------
throws this exception:
----------
socket.gaierror: (11001, 'getaddrinfo failed')
----------
note that this is the same as your "telnetlib.Telnet.open". So, you
will want to use TRY: and EXCEPT: to catch those errors, as in the
example code below...
----------
import telnetlib
try:
    c = telnetlib.Telnet("blah")
except socket.gaierror:
    print "host could not be found."
----------
Note that the "socket.gaierror" is thrown when the hostname is invalid,
and "socket.error" is thrown when the connection is refused. If you try
to perform a "read" operation on a closed socket, you'll get thrown a
"EOFerror" or you may get a Null value.  You'll have to handle each
case. I would suggest referring to the documentation at
"http://www.python.org/doc/2.4/lib/telnet-objects.html" for more
information about errors / exceptions returned. If there is none
specified, chances are, it will throw a "socket.*" exception, or a Null
value.




More information about the Python-list mailing list