Search code examples
pythontelnetlib

Detect a closed connection in python's telnetlib


I'm using python's telnetlib to connect to a remote telnet server. I'm having a hard time detecting if the connection is still open, or if the remote server closed it on me.

I will notice the connection is closed the next time I try to read or write to it, but would like to have a way to detect it on demand.

Is there a way to send some sort of an 'Are You There' packet without affecting the actual connection? The telnet RFC supports an "are you there" and "NOP" commands - just not sure how to get telnetlib to send them!


Solution

  • You should be able to send a NOP this way:

    from telnetlib import IAC, NOP
    
    ... 
    
    telnet_object.sock.sendall(IAC + NOP)