无法清除python3.X中的pexpect缓冲区


9

我正在使用Pexpect模块连接到远程服务器。我可以成功发送和检索响应。我正在尝试通过期待垃圾来清除缓冲区,并假定它将清除缓冲区,但实际上并没有清除缓冲区。

下面是我的示例代码

import pexpect
obj = pexpect.spawn("telnet 172.16.250.250", maxread=8192)

obj.sendline("")
result = obj.expect(expected, timeout=3) --> getting output here `OUTPUT 1`
obj.sendline("1")
time.sleep(3)
try:
    obj.expect("Asdfgdsad", timeout=2)  --> I am expecting to clear buffer here but it did not

except pexpect.TIMEOUT:
    pass
print("buffer is", obj.buffer) . --> This is printing output `OUTPUT 1` as I have meniotned

我在这里做错什么了?我正在使用python3.7。如果我没记错的话,它在python2.X中正常工作

Answers:


3

您可以通过显式读取IIRC来清除pexpects缓冲区。

flush = ''
while not obj.expect(r'.+', timeout=5):
    flush += obj.match.group(0)
By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.