我遇到了问题,因为我正在尝试使用 telnet 连接到我的树莓派,但是当涉及到读取用户名条目的部分时,我收到一个错误,我已将其粘贴到下面。
#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
请问有人可以告诉我为什么收到错误消息以及如何修复它吗?
谢谢
最佳答案
TypeError: Type str doesn't support the buffer API
这是一个棘手的错误 - 它告诉您的信息足以为您提供帮助,但前提是您知道问题是什么。
在 Python 3 中,字节之间有明显的区别:
b'This is a byte string'
以及常规 unicode 或 str
字符串:
'This is a regular unicode string'
当美国是互联网上唯一的国家并且 ASCII 是网络的通用编码时,这是一个没有人真正关心的重要区别。但现在我们需要表示的字符远远多于 0-254 个字符。这就是unicode进来。
现在,在大多数语言中,它们都会像二进制数据一样抛出字符串,反之亦然,这可能会导致各种奇怪和意外的怪癖。 Python3 尝试做正确的事情(tm),并且通过决定明确区分字节和文本而取得了很大的成功。字节只是二进制数据,您可以将这些字节解码为您想要的任何编码(UTF、ASCII 等疯狂的编码),但只有在向用户显示它时才应该这样做。否则,您将传递二进制数据。
我告诉你这个故事是为了告诉你:
bot.read_until("login: ", timeout=None)
具有以下unicode str
- “登录:”
。 str
不支持缓冲区接口(interface),但 bytes
支持。
使用以下缩写词帮助您记住:BADTIE
B ytes
一个重新
D 编码
文本分机
我是
电子编码
并像 bot.read_until("login: ".encode(), timeout=None)
那样编写。 您还需要修复其他字符串。另一个可行的选择是将其更改为 b"login: "
但我从未尝试过。
关于python - 为什么在 Python Shell 中使用 Telnet 时出现此错误消息,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23221123/