给出一个交互式python脚本
#!/usr/bin/python
import sys
name = raw_input("Please enter your name: ")
age = raw_input("Please enter your age: ")
print("Happy %s.th birthday %s!" % (age, name))
while 1:
r = raw_input("q for quit: ")
if r == "q":
sys.exit()
我想通过expect脚本与它进行交互
#!/usr/bin/expect -f
set timeout 3
puts "example to interact"
spawn python app.py
expect {
"name: " { send "jani\r"; }
"age: " { send "12\r"; }
"quit: " { send "q\r"; }
}
puts "bye"
期望脚本似乎没有与python应用程序进行交互。
python或期望代码有问题吗?
答案 0 :(得分:0)
您需要的是3种不同的期望电话:
#!/usr/bin/expect -f
set timeout 3
puts "example to interact"
spawn python app.py
expect "name: " { send "jani\r" }
expect "age: " { send "12\r" }
expect "quit: " { send "q\r" }
原因是expect
命令不像循环那样工作。一旦它处理输入/输出,它就会继续。
答案 1 :(得分:0)
你为什么需要期待?
python app.py <<END_INPUT
jani
12
q
END_INPUT