我可以在Python 2.7上清除Telnetlib中的输出缓冲区

时间:2013-05-01 04:59:42

标签: python telnet telnetlib

我使用telnetlib在写入服务器

的最后一个命令后打印输出
tn.write(cmd_login)
tn.write(cmd...)
tn.write(cmd_last)
print tn.expect([word],timeout)[-1]

然而,当我打印期望的回报时,它还显示我之前写入服务器的结果(例如:cmd_login cmd ...) 无论如何只在tn.write(cmd_last)之后打印结果?

2 个答案:

答案 0 :(得分:0)

我建议在每个Telnet.write之后使用Telnet.read_until。对于read_until的预期参数,可以使用telnet提示符。最后,命令应该从输出中减去。我不知道更好的方法。它看起来像这样:

tn.write(cmd_login)
tn.read_until(prompt)
tn.write(cmd...)
tn.read_until(prompt)
tn.write(cmd_last)
output = tn.read_until(prompt)
cmdIndex = output.find(cmd_last)
output = output[cmdIndex + len(cmd_last):]
print output

答案 1 :(得分:0)

我通过运行read_very_lazy()+ read_until解决了它:

tn.read_very_lazy()
tn.write(b'the_command\r\n')
tn.read_until(b'\r\n', timeout=2)

但是,我并没有对其进行很好的测试,声称它没有任何负面影响。