1

I'm working on a project that involves sending information from an ESP8266 by TCP/IP over telnet to python, and to be honest its my first time using telnetlib. I wrote this small code for testing, but when I execute it, it returns "'in ' requires string as left operand, not bytes". Can someone help me out please?

import telnetlib
HOST="192.168.0.67"
tn = telnetlib.Telnet(HOST, "23")

tn.write("Hello World\n")
print (tn.read_all())

1 Answers1

0

The read_all() method in telnetlib returns bytes rather than a string. You can verify this by reading the documentation for telnetlib.

print() takes a string as its argument - as the error you mentioned references. You'll need to convert the bytes returned by telnetlib.

Try using the str() function:

print (str(tn.read_all(), 'UTF-8'))

the UTF-8 argument specifies the encoding of the string, in this case 8 bit Unicode.

As an aside, Telnet is a deprecated protocol for doing remote login. It's a bit pointless to use it just to transfer data. You're better off using a raw TCP connection. Telnet is a thin layer above TCP that adds complexity and overhead you almost certainly do not need.

romkey
  • 6,218
  • 3
  • 16
  • 12