[英]Why Python pexpect does not match the expected prompt with newline here
I am trying to execute client.py from run.py. 我试图从run.py执行client.py. The client.py prompts for an input.
client.py提示输入。 I have written a simple pexpect code but it doesn't match the prompt and hangs.
我编写了一个简单的pexpect代码,但它与提示符不匹配并挂起。
Here is my code 这是我的代码
input = raw_input("Please data, default [ /Anything ]:\n")
if input == "Admin":
print "Welcome Admin"
else:
print "Welcom Guest"
import pexpect
child = pexpect.spawn ('python client.py')
child.expect('Please data, default [ /Anything ]:\n')
child.sendline ('anonymous')
Here is an another try with expect_exact, it did not help me.. 这是另一个与expect_exact的尝试,它没有帮助我..
import pexpect
child = pexpect.spawn ('python client.py')
child.expect_exact('Please data, default [ /Anything ]:\n')
child.sendline ('anonymous')
Python 2.7.2 (default, May 13 2014, 12:53:14)
[GCC 4.4.7 20120313 (Red Hat 4.4.7-4)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import pexpect
>>> pexpect.__version__
'3.2'
>>>
您可能希望使用expect_exact
而不是expect
,因为后面的方法需要一个正则表达式,其中[
是一个特殊字符。
The problem is your \\n
character. 问题是你的
\\n
字符。 As explained here . 正如解释在这里 。 You are sending a
\\n
but pexpect sees that as a \\r\\n
, so you have to tell run.py
to expect \\r\\n
instead of just \\n
: 你正在发送一个
\\n
但是pexpect认为它是一个\\r\\n
,所以你必须告诉run.py
期望\\r\\n
而不仅仅是\\n
:
import pexpect
child = pexpect.spawn ('python client.py')
child.expect_exact('Please data, default [ /Anything ]:\r\n')
child.sendline ('anonymous')
Leave the client.py
just as it is (do not add \\r
to the raw_input
string there). 保持
client.py
就是这样(不要在那里添加\\r
到raw_input
字符串)。
你可以在Anything
方面做一些Anything
,然后继续你的代码。
child.pexpect('Anything')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.