类型错误:';在<;字符串>;而通过python telnet发送cmd

类型错误:';在<;字符串>;而通过python telnet发送cmd,python,python-3.x,telnet,telnetlib,Python,Python 3.x,Telnet,Telnetlib,我试图通过python telnet发送ls/cmd import telnetlib tel = telnetlib.Telnet('10.10.0.1'.'1234') tel.write('ls / ') 但我有一个错误: if IAC in buffer: TypeError: 'in <string>' requires string as left operand, not bytes 如果IAC在缓冲区中: TypeError:“in”需要字符串作为左操作数,而不是

我试图通过python telnet发送
ls/
cmd

import telnetlib
tel = telnetlib.Telnet('10.10.0.1'.'1234')
tel.write('ls / ')
但我有一个错误:

if IAC in buffer:
TypeError: 'in <string>' requires string as left operand, not bytes
如果IAC在缓冲区中:
TypeError:“in”需要字符串作为左操作数,而不是字节

您需要将字符串编码为ASCII:

tel.write(('ls / ').encode('ascii'))

在Python3中,
str
类将字符串存储为unicode,您需要显式地将它们转换为
ascii
。基本上,您需要一个字节字符串,其中每个字符都是ASCII字符,而不是Unicode字符。

请参见编辑,字符串是以Unicode形式传递的,您需要为ASCII提供显式编码以传入telnetlib@MicrosoftCProg