为什么我在 Python Shell 中使用 Telnet 时会收到此错误消息 [英] Why Do I Get This Error Message When Using Telnet In Python Shell

查看:24
本文介绍了为什么我在 Python Shell 中使用 Telnet 时会收到此错误消息的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了一个问题,因为我正在尝试使用 telnet 连接到我的 raspberry pi,但是当涉及到读取用户名条目的部分时,我收到一个错误,我已将其粘贴在下面.

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 + "
")
bot.read_until("Password: ")
bot.write(pass_ + "
")
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'

这是一个重要的区别,当美国是互联网上唯一的国家并且 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
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.

这篇关于为什么我在 Python Shell 中使用 Telnet 时会收到此错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆