I am using Pexpect module to connect to remote server. I can successfully send and retrieve response. I am trying to clear a buffer by expecting something junk and assuming it will clear the buffer but actually it is not clearing the buffer.
Below is my sample code
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
I am doing something wrong here?? I am using python3.7 . If I remember correctly It was working correctly in python2.X
You can clear pexpects buffer by explicitly reading it, IIRC.
flush = ''
while not obj.expect(r'.+', timeout=5):
flush += obj.match.group(0)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With