Dominik
Dominik

Reputation: 331

Python telnetlib ending session

If I am using Python telnetlib, is there a way to close the telnet session if that device does not support nothing to terminate telnet session, so no ctrl+something or quit or anything like that.

I need this so that I could use read.all

Upvotes: 1

Views: 8897

Answers (1)

tdelaney
tdelaney

Reputation: 77347

Network sockets let you shutdown write and/or read channels to let the other side know that you have finished that part of the conversation. For a telnet server, shutting down the write channel is an exit. It should finish sending whatever is in the send pipeline and then close the connection completely. That close is an EOF and read_all should return. So, assuming you've already got a connection called tn

tn.get_socket().shutdown(socket.SHUT_WR)
data = tn.read_all()
tn.close()

Upvotes: 2

Related Questions