在Python Shell中使用Telnet时为什么会收到此错误消息

问题描述:

我遇到了一个问题,因为我试图使用telnet连接到我的树莓派,但是当涉及到读取用户名条目的部分时,出现了一个错误,我已经在下面粘贴了.

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()

我收到以下错误消息:

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?

谢谢

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.

在Python 3中,字节之间有明显的区别:

In Python 3 there is a distinct difference between bytes:

b'This is a byte string'

以及常规unicode或str字符串:

And regular unicode, or str strings:

'This is a regular unicode string'

这是一个重要的区别,当美国是Internet上唯一的国家,而ASCII是网络的编码通用语言时,没有人真正在乎.但是,现在我们需要代表的字符总数超过了0-254个.这是 unicode 出现的地方.

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.

现在,在大多数语言中,它们都好像是二进制数据一样将字符串抛出,反之亦然,这可能导致各种奇怪和意外的怪癖. Python3试图做正确的事情(tm),并通过决定明确区分字节和文本而大获成功.字节只是二进制数据,您可以将这些字节解码为所需的任何编码(UTF,ASCII,有些疯狂)-但只有在向用户显示时才应这样做.否则,二进制数据就是您要传递的内容.

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)

具有以下 unicode str-"login: ". str不支持缓冲区接口,但bytes则支持.

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

使用以下首字母缩写词来帮助您记住: BADTIE

Use the following acronym to help you remember: BADTIE

B ytes
A 重新
D 环保
T 分机
s
E 已编码

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

并像bot.read_until("login: ".encode(), timeout=None)那样编写. 您还需要修复其他字符串.另一个可行的选择是将其更改为b"login: ",但我从未尝试过.

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.