在pexpect中与python脚本交互

在pexpect中与python脚本交互,python,pexpect,Python,Pexpect,我正在尝试使用pexpect自动化交互式python脚本。但在控件返回到pexpect后,它不会继续 下面是一个模拟脚本,试图模拟相同的东西 ---------------- python script(script.py) ----------------- def issue_command(): print "********** in issue_command ********" def dummy_check(): print "**********

我正在尝试使用pexpect自动化交互式python脚本。但在控件返回到pexpect后,它不会继续

下面是一个模拟脚本,试图模拟相同的东西

---------------- 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()
-------------------------------------------------------------------
我希望输出结果如下

$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 ********
$
相反,它不会打印第二行,即pexpect无法交互 在脚本返回之后,在这两者之间

$ python pexp.py
./script.py
********** in dummy_check ********
Now Press enter for next command -----> It ignored sendline() and did not execute issue_command function.
$
我还尝试直接在pexpect.spawn()中传递脚本(script.py),而不是创建另一个shell(/bin/bash)。这没有帮助。 我不确定我做错了什么。谁能给点建议吗


谢谢。

您的pexpect工作正常,但您尚未要求从spawn对象获得更多输出

按原样运行您编写的代码,我得到以下输出:

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

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


********** in issue_command ********
$
如果在
pexp.py
的末尾添加另一个
pobj.expect()
调用,则可以获得剩余的输出。特别是,使用pexpect搜索常量
pexpect.EOF
将使您的spawn对象查找文件的结尾(当脚本完成时),并将输出记录到stdout。以下是您的代码和附加代码:

import-pexpect
导入系统
def快速_测试():
pobj=pexpect.spawn('/bin/bash')
pobj.logfile=sys.stdout
sendline('python script.py')
expect(“现在按enter键执行下一个命令”)
打印“\n第一部分完成。现在执行oil命令”
pobj.sendline()
pobj.expect(peexpect.EOF)#