Question

Below is a minimal example of my code, which If you run, should recreate the problem I have. One thing I have encounter that the error does not appear if my raspberry pi (the thing I am using teller to connect to) is turned off or the internet cable is unplugged.

#IMPORTS
import time
import telnetlib
import sys
import getpass


#TELNET
user = input("Please Enter Your Username: ")
time.sleep(0.4)
pass_ = input("Please Enter Your Password: ")

bot = telnetlib.Telnet("192.168.1.128")
bot.read_until("login: ".encode(),  timeout=None)
bot.write(user + "\n")
bot.read_until("Password: ".encode(),  timeout=None)
bot.write(pass_ + "\n")
bot.write("cd PiBits/ServoBlaster")

I get the following error message:

Traceback (most recent call last):
  File "/Users/kiancross/Desktop/PROJECTS/RASPBERRY_PI/ROBOT/CONTROLLER_GUI/RPi_BOT_CONTROLLER.py", line 17, in <module>
    bot.write(user + "\n")
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/telnetlib.py", line 289, in write
    if IAC in buffer:
TypeError: 'in <string>' requires string as left operand, not bytes

I was running the following code which you can see in this post but as you can see from the answer I changed it to the code above which resulted in the above error message.

I should say that the code is no where near finished and a lot of things are just to test until I get telnet working!

I have tried the fix for the older version of python mentioned in this post.

I have used print(type(user)) to see what kind the variables are and they are strings. I have tried bot.write((user + "\n").encode(latin-1)) as shown in this fix, but the error message still appears. I have also tried encoding in unicode. I understand that part of that line is encoded wrong but I don't know if it is the variables or the "\n". I have tried doing bot.write(user + b"\n") but this get rid of the error.

If anybody has any other methods that could stop this error, I would greatly appreciate them.

Thanks

P.S Im running on python 3.4.0

EDIT

I have tried this:

bot = telnetlib.Telnet("192.168.1.128")
bot.write(user + "\n".encode('ascii'))
bot.read_until(b"Password: ")
bot.write((pass_ + "\n").encode('ascii'))

But I still get the same error.

When I do:

bot = telnetlib.Telnet("192.168.1.128")
bot.read_until(b"login: ")
bot.write(user + b"\n")
bot.read_until(b"Password: ")
bot.write(pass_ + b"\n")

I get a different error:

Traceback (most recent call last):
  File "/Users/kiancross/Desktop/PROJECTS/RASPBERRY_PI/ROBOT/CONTROLLER_GUI/RPi_BOT_CONTROLLER.py", line 23, in <module>
    bot.write(user + b"\n")
TypeError: Can't convert 'bytes' object to str implicitly

All the error seem to say that It needs to be a string but my variables are already strings.

Thanks

Was it helpful?

Solution

The telnet interface probably wants to work in bytes not Unicode. I have tested this code on Python3 and it avoids the error:

import telnetlib
bot = telnetlib.Telnet("127.0.0.1", 22)
user = "dobbs"
bot.write((user + "\n").encode('ascii'))
print(bot.read_all())
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top