How to know if connection is active when using telnetlib?

  • Thread starter Wojciech Halicki-Piszko
  • Start date
W

Wojciech Halicki-Piszko

How to know if connection is active after telnetlib.Telnet.open(host,port)?
 
E

Eddie Corns

Wojciech Halicki-Piszko said:
How to know if connection is active after telnetlib.Telnet.open(host,port)?

If open() doesn't throw an exception then you should have a connection you can
start reading/writing with. Unless you have some special meaning for
'active'?

I'm just basing this on reading telnetlib.py.

Eddie
 
S

sp1d3rx

Well, running this code:
--------------------
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.
 
W

Wojciech Halicki-Piszko

If open() doesn't throw an exception then you should have a connection you can
start reading/writing with. Unless you have some special meaning for
'active'?

I'm just basing this on reading telnetlib.py.

Eddie

Well, in fact what I meant is: I won't to check wether established
connection is still active. I have a reading thread and when I send 'quit'
command to a server it (guess what:)) disconnects. So I get plenty of
unwanted output, I want to avoid this. So the problem is how can I inform
my reader thread it is not longer wanted when _server_ disconnects me? To
my defense: repetition is mother of science (in case you did answer and I
did not get it first time).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,534
Members
45,007
Latest member
OrderFitnessKetoCapsules

Latest Threads

Top