如何将 telnet 作为文本文件而不是二进制文件打开 [英] How to open telnet as a textfile rather than a binary file

查看:37
本文介绍了如何将 telnet 作为文本文件而不是二进制文件打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我试图在 telnet 中使用 read_until 方法,但后来遇到了错误:

So I was trying to use the read_until method in telnet but then ran into the error:

Traceback (most recent call last):
File "c:\Users\Desktop\7DTD Bot\test.py", line 44, in <module>
tn.read_until("Please enter password:")
File "C:\Users\AppData\Local\Programs\Python\Python37\lib\telnetlib.py", 
line 302, in read_until
i = self.cookedq.find(match)
TypeError: argument should be integer or bytes-like object, not 'str'

我几乎可以肯定这是因为 telnet 是作为二进制文件而不是文本文件打开的.这是我首先打开它的方式:

I'm almost sure that this is because the telnet is being opened as a binary file rather than as a text file. Here's how I opened it in the first place:

HOST = config.get("telnet", "telnet_host")
PORT = config.get("telnet", "telnet_port")
PASS = config.get("telnet", "telnet_pass")

tn = telnetlib.Telnet(HOST, PORT)
tn.read_until("Please enter password:")

推荐答案

我不确定是否有更好的方法来做到这一点,但我只是将字符串转换为字节字符串来解决这个问题:

I'm not sure if there's a better way to do it but I just converted the string to a bytestring to fix this problem:

tn.read_until(b"Please enter password:")

也感谢减一!

这篇关于如何将 telnet 作为文本文件而不是二进制文件打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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