pythontelnettelnetlib

How to get the complete response from a telnet command using python?


I am trying to use python 3.10.9 on windows to create a telnet session using telnetlib, but I have trouble to read the complete response.

I create a telnet session like

session = telnetlib.Telnet(host, port, timeout)

and then I write a command like

session.write(command + b"\n")

and then I wait some really long time (like 5 seconds) before I try to read the response using

session.read_some()

but I only get half of the response back!

The complete response is e.g.

Invalid arguments

Usage: $IMU,START,<SAMPLING_RATE>,<OUTPUT_RATE>
  where SAMPLING_RATE = [1 : 1000] in Hz
        OUTPUT_RATE   = [1 : SAMPLING_RATE] in Hz

but all I read is the following:

b'\x1b[0GInvalid arguments\r\n\r\nUsage: $IMU,START,<'

More than half of the response is missing! How to read the complete response in a non-blocking way?

Other strange read methods:

I have not the slightest idea what all these different read methods are for. The documentation is not helping at all.

But read_very_eager seems to work sometimes. But sometimes I get a response like

F
FI
FIL
FILT
FILTE
FILTER

and so on. But I am reading only once, not adding the output myself!

Maybe there is a more simple-to-use module I can use instead if telnetlib?


Solution

  • Have you tried read_all(), or any of the other read_* options available?

    Available functions here.