我用telnet连接到我的服务器,它用消息回答我,并且在每条消息的末尾附加了hex00(空字符),不能读取。我尝试了一遍又一遍地搜索,但似乎无法成功,一个简单的例子:
from telnetlib import Telnet
connection = Telnet('localhost', 5001)
connection.write('aa\n')
connection.read_eager()
这将返回一个输出:
^{pr2}$但是应该有类似的东西:
'Fail - Command aa not found.\n\r\0'
有没有办法得到这个字符串的结尾字符?如果故意漏掉字符,我能得到字节作为输出吗?在
00字符在那里:
这段代码(http://www.opensource.apple.com/source/python/python-3/python/Lib/telnetlib.py)似乎只是忽略了空字符。这真的是正确的行为吗?在
然后由read_until调用进程_rawq
^{pr2}$我还想接收空字符。在我的特殊情况下,它标志着多行消息的结束。在
所以答案似乎是,这是编写库代码时的预期行为。在
FWIWhttps://support.microsoft.com/en-us/kb/231866状态:
以及
当我试图使用telnet从RS232-TCP/IP转换器获取数据时,我也遇到了同样的问题——telnetlib会从消息中抑制每0x00次。正如Fredrik Johansson很好地回答的那样,这就是telnetlib的实现方式。在
一种解决方案是重写telnetlib的Telnet类中的
process_rawq()
函数,该类不占用所有空字符:然后重写Telnet类的方法:
^{pr2}$这就解决了我的问题。在
相关问题 更多 >
编程相关推荐