2016-04-29 11 views
0

Ich versuche, ein interaktives Python-Skript mit Pexpect zu automatisieren. Aber es geht nicht weiter, nachdem die Kontrolle zurückkehrt.Interagieren mit einem Python-Skript innerhalb von Pexpect

Hier ist ein Mock Up Skript versuchen, das gleiche zu simulieren.

---------------- python script(script.py) ----------------- 
def issue_command(): 
     print "********** in issue_command ********" 

def dummy_check(): 
     print "********** in dummy_check ********" 
     raw_input("Now Press enter for next command") 
     issue_command() 

if __name__ == "__main__": 
    dummy_check() 

--------------------------------Pexpect script(pexp.py)----------------------- 
import pexpect 
import sys 

def quick_test(): 

     pobj = pexpect.spawn("/bin/bash") 
     pobj.logfile = sys.stdout 
     pobj.sendline("script.py") 
     pobj.expect("Now Press enter for next command") 
     print "\n1st part is done. Now execute the oil command" 
     pobj.sendline() 
if __name__ == "__main__": 
    quick_test() 
------------------------------------------------------------------- 

Ich erwarte, dass der Ausgang folgt.

$python pexp.py 
********** in dummy_check ******** 
Now Press enter for next command -------------------> It should wait here. Upon pressing enter it should print the next line. 
********** in issue_command ******** 
$ 

Stattdessen drucken Sie es nicht die zweite Zeile heißt die pexpect nicht mit dem Skript interagieren konnte, nachdem er zurück dazwischen.

$ python pexp.py 
./script.py 
********** in dummy_check ******** 
Now Press enter for next command -----> It ignored sendline() and did not execute issue_command function. 
$ 

Ich habe auch versucht, das Skript (script.py) direkt in der pexpect.spawn Passing() anstelle einer anderen Schale zu schaffen (/ bin/bash). Es hat nicht geholfen. Ich bin mir nicht sicher, was ich falsch mache. Kann jemand bitte beraten?

Danke.

Antwort

0

Ihr Pexpect funktioniert ordnungsgemäß, aber Sie haben noch keine weitere Ausgabe vom Spawn-Objekt angefordert.

Ausführen des Codes Sie können wie geschrieben, erhalte ich die folgende Ausgabe:

********** in dummy_check ******** 
Now Press enter for next command 
1st part is done. Now execute the oil command 

$ 

Wenn Sie einen anderen pobj.expect() Aufruf zum Ende pexp.py hinzufügen, können Sie die restlichen Ausganges. Insbesondere wird durch die Verwendung der pexpect-Suchkonstante pexpect.EOF Ihr Spawn-Objekt nach dem Ende der Datei suchen (wenn das Skript abgeschlossen ist) und die Ausgabe in stdout protokollieren. Hier ist der Code mit dem Zusatz:

import pexpect 
import sys 

def quick_test(): 
    pobj = pexpect.spawn('/bin/bash') 
    pobj.logfile = sys.stdout 
    pobj.sendline('python script.py') 
    pobj.expect("Now Press enter for next command") 
    print "\n1st part is done. Now execute the oil command" 
    pobj.sendline() 
    pobj.expect(pexpect.EOF) # <-- wait until we hit the end of the file 

if __name__ == '__main__': 
    quick_test() 

Ausführen dieses die folgende Ausgabe gibt:

********** in dummy_check ******** 
Now Press enter for next command 
1st part is done. Now execute the oil command 


********** in issue_command ******** 
$