Why Do I Get This Error Message When Using Telnet In Python Shell [duplicate]

半城伤御伤魂 提交于 2019-12-08 08:43:08

问题


I am having a problem because I am trying to connect to my raspberry pi using telnet but when it comes to the part where it is reading for the username entry I get an error which I have pasted bellow.

#IMPORTS
from tkinter import *
import time
import telnetlib
import sys
import getpass
import tkinter.messagebox


#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: ")
bot.write(user + "\n")
bot.read_until("Password: ")
bot.write(pass_ + "\n")
bot.write("cd PiBits/ServoBlaster")


#DEFINITIONS


#STUFF
master = Tk()

#LEFT
left = Scale(master,from_=0,to=249,length=550,width=25,tickinterval=152,sliderlength=30)
left.pack()
left.set(152)
left.grid(row=0, column=2)

#RIGHT
right = Scale(master,from_=0,to=249,length=550,width=25,tickinterval=152,sliderlength=30)
right.pack()
right.set(152)
right.grid(row=0, column=12)

#MIDDLE

mid = Scale(master,from_=0,to=249,length=550,width=25,tickinterval=152,sliderlength=30)
mid.pack()
mid.set(152)
mid.grid(row=0, column=7)

#RUN CANVAS
mainloop()

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 16, in <module>
    bot.read_until("login: ", timeout=NONE)
  File "/Library/Frameworks/Python.framework/Versions/3.4/lib/python3.4/telnetlib.py", line 304, in read_until
    i = self.cookedq.find(match)
TypeError: Type str doesn't support the buffer API

Please can somebody tell me why I am getting the error message and how I can fix it?

Thanks


回答1:


TypeError: Type str doesn't support the buffer API

Is kind of a tricky error - it tells you just enough to be helpful, but only if you know what the problem is.

In Python 3 there is a distinct difference between bytes:

b'This is a byte string'

And regular unicode, or str strings:

'This is a regular unicode string'

This is an important distinction that nobody really cared about when the USA was the only country on the Internet and ASCII was the coding franca of the web. But now we have a whole heck of a lot more than 0-254 characters that we need to represent. This is where unicode comes in.

Now, in most languages they kind of throw around strings as if they were binary data, and vice versa, which can cause all kinds of weird and unexpected quirks. Python3 has tried to do the Right Thing(tm), and largely succeeded, by deciding to make explicit the distinction between bytes and text. Bytes are just binary data, and you can decode those bytes into whatever encoding (UTF, ASCII, something crazy) you want - but you should only do that when you're displaying it to the user. Otherwise, binary data is what you're passing around.

I told you that story to tell you this:

bot.read_until("login: ", timeout=None)

has the following unicode str - "login: ". str does not support the buffer interface, but bytes do.

Use the following acronym to help you remember: BADTIE

B ytes
A re
D ecoded
T ext
I s
E ncoded

and write it like bot.read_until("login: ".encode(), timeout=None). You'll need to fix your other strings as well. Another option that should work is just changing it to b"login: " but I've not ever tried that.



来源:https://stackoverflow.com/questions/23221123/why-do-i-get-this-error-message-when-using-telnet-in-python-shell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!