Impossibile cancellare il buffer pexpect in python3.X


9

Sto usando il modulo Pexpect per connettermi al server remoto. Posso inviare e recuperare correttamente la risposta. Sto cercando di cancellare un buffer aspettandomi qualcosa di spazzatura e supponendo che cancellerà il buffer ma in realtà non sta cancellando il buffer.

Di seguito è riportato il mio codice di esempio

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

Sto facendo qualcosa di sbagliato qui ?? Sto usando python3.7. Se ricordo bene Funzionava correttamente in python2.X

Risposte:


3

È possibile cancellare il buffer pexpects leggendolo esplicitamente, IIRC.

flush = ''
while not obj.expect(r'.+', timeout=5):
    flush += obj.match.group(0)
Utilizzando il nostro sito, riconosci di aver letto e compreso le nostre Informativa sui cookie e Informativa sulla privacy.
Licensed under cc by-sa 3.0 with attribution required.