Search code examples
pythoninteractivetelnetlib

telnetlib - interactive console vs script


The problem is, my script won't work (it's printing empty lane), but it works in python interactive console.

import telnetlib

tn = telnetlib.Telnet("killermud.pl", 4000)
data = tn.read_very_eager()
data = data.decode()
print(data)
tn.close()

What is the reason of such behavior?


Solution

  • I just took a look at the documentation for the read_very_eager method, which says:

    Read all data available already queued or on the socket, without blocking.

    It is likely that at the time you call this method that there is no data "already available or queued on the socket", so you're getting nothing back. You probably want to use something like the read_until method, which will read data until it finds a specific string. For example:

    data = tn.read_until('Podaj swoje imie')