This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: Cannot re-open an existing telnet session
Type: enhancement Stage: resolved
Components: Versions: Python 3.7
Status: closed Resolution: not a bug
Dependencies: Superseder:
Assigned To: Nosy List: ckreddy, eric.smith, r.david.murray
Priority: normal Keywords:

Created on 2018-06-08 12:16 by ckreddy, last changed 2022-04-11 14:59 by admin. This issue is now closed.

Messages (6)
msg319056 - (view) Author: Chandrakanth Reddy (ckreddy) Date: 2018-06-08 12:16
i see from the below link it says "cannot re-open an already existing telnet instance" using Telnetlib. Can this be fixed in the later versions of python or is there any work around for this. 

I'm surprised that this is something which PERL supports and not python .

When I try to open a new session which is already opened the telnet server asks whether to open in only or mode and when i try to write 'a' it doesn't take and program fails.

my code:
tn = telnetlib.Telnet(HOST,PORT)
print("established connection")
print(tn.read_until(">> ".encode('ascii')))

established connection
b'\r\n a. Connect to Port read/write\r\n b. Connect to Port read only\r\n c. Do not connect, drop this connection request\r\n d. Look at port log file\r\n>> '
msg319063 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-06-08 13:49
Which link are you referring to?

I don't see any code you've provided where you're reopening a connection. It looks like you're opening a new connection, reading, writing, and nothing else.

It also looks like the output you show is coming from the remote server, not anything on the client side in Python.

And, as the example code in the documentation shows, you should probably be using a "with" statement.
msg319070 - (view) Author: Chandrakanth Reddy (ckreddy) Date: 2018-06-08 14:45
Hi Eric,

Apologize for not providing the link. Below is the link which i was referring to:

I have an already opened telnet session(established by some other user). Now if I again try to open a telnet session to the same serve, my server will populate me with some options like below:

a. Connect to Port read/write
b. Connect to Port read only
c. Do not connect, drop this connection request
d. Look at port log file

So python telnetlib module doesn't handle these options even though i use below code. 
#establishing connectino to the remote host 
tn = telnetlib.Telnet(HOST,PORT)
# remote server will populate the above given options and stop at '>>', so i'm using the below line to read '>>' and write 'a'
print(tn.read_until(">> ".encode('ascii')))

i have created this issue after trying all possible ways and searching the web for a whole day. 
Let me know if there is any way to handle this.
msg319071 - (view) Author: Eric V. Smith (eric.smith) * (Python committer) Date: 2018-06-08 14:47
This sounds like an application-level issue, not a telnetlib problem. As far as I can tell, you're opening a new connection, not reusing an existing connection.
msg319074 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-06-08 15:05
telnetlib provides a low level interface to the telnet protocol.  The dialog you mention appears to be transmitted on the telnet connection, so it is unlikely there is any bug or missing feature in telnetlib that would affect your problem.

The sentence in the docs you appear to be referring to ("Do not reopen an already connected instance.") is just telling you that once you have used to telnetlib to open a connection, don't call open again.  It has nothing to say about a telnet connection made by some other program.

So, you have an application level problem and should seek help on the python-list mailing list or some other general help forum.  Unless you can identify something *specific* in telnetlib (bug or missing feature) that prevents you from solving your problem, I don't think there's anything more to be done here.

(As an aside, have you tried sending /r/n after the a?)
msg319075 - (view) Author: R. David Murray (r.david.murray) * (Python committer) Date: 2018-06-08 15:09
To clarify: don't call open again *on that telnetlib.Telnet object*.  You can certainly have more than one open connection using different telnetlib.Telnet instances, though it might be a bit challenging to manage them :)
Date User Action Args
2022-04-11 14:59:01adminsetgithub: 77987
2018-06-08 15:09:14r.david.murraysetmessages: + msg319075
2018-06-08 15:06:27r.david.murraysetstatus: open -> closed
resolution: not a bug
stage: resolved
2018-06-08 15:05:12r.david.murraysetnosy: + r.david.murray
messages: + msg319074
2018-06-08 14:47:53eric.smithsetmessages: + msg319071
2018-06-08 14:45:35ckreddysetmessages: + msg319070
2018-06-08 13:49:42eric.smithsetnosy: + eric.smith
messages: + msg319063
2018-06-08 12:16:04ckreddycreate