【问题标题】:TypeError: 'in <string> while send cmd via python telnetTypeError: 'in <string> while send cmd via python telnet
【发布时间】:2021-04-09 07:19:01
【问题描述】:

我尝试通过 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

【问题讨论】:

    标签: python python-3.x telnet telnetlib


    【解决方案1】:

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

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

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

    【讨论】:

    • 查看编辑,字符串作为 unicode 传递,您需要向 ascii 提供显式编码以传递 telnetlib。 @MicrosoctCprog
    猜你喜欢
    • 2018-12-11
    • 2018-11-30
    • 1970-01-01
    • 2019-10-04
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 2012-08-16
    • 2022-10-13
    相关资源
    最近更新 更多