在python shell中运行telnet时出现错误消息 [英] Error message when running telnet in python shell
问题描述
下面是我的代码的一个最小示例,如果您运行它,应该会重现我的问题。我遇到的一件事是,如果我的树莓派(我正在使用出纳员连接的东西)关闭或拔掉了互联网电缆,则不会出现错误。
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")
我收到以下错误消息:
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.
我应该说代码没有完成的地方,直到telnet正常工作之前,很多事情都需要测试!
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!
我已经尝试了这篇帖子。
我使用了 print(type(user))
查看变量是什么类型,它们是字符串。我已尝试如此修复程序中所示 bot.write((user + \n)。encode(latin-1))
,但错误消息仍然出现。我也尝试过用unicode编码。我知道该行的一部分编码错误,但是我不知道它是变量还是 \n
。我曾尝试做 bot.write(user + b \n)
,但这可以消除错误。
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.
谢谢
在python 3.4.0上运行的PS Im
P.S Im running on python 3.4.0
EDIT
我有尝试过这个:
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.
当我这样做时:
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")
我遇到了另一个错误:
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.
谢谢
推荐答案
telnet接口可能希望以非Unicode字节工作。我已经在Python3上测试了此代码,并且避免了该错误:
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())
这篇关于在python shell中运行telnet时出现错误消息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!