Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unable to clear pexpect buffer in python3.X

Tags:

python

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

like image 885
Sumit Avatar asked Feb 04 '20 08:02

Sumit


1 Answers

You can clear pexpects buffer by explicitly reading it, IIRC.

flush = ''
while not obj.expect(r'.+', timeout=5):
    flush += obj.match.group(0)
like image 190
Aiyion.Prime Avatar answered Oct 28 '22 18:10

Aiyion.Prime