[英]Why Python pexpect does not match the expected prompt with newline here
我试图从run.py执行client.py. client.py提示输入。 我编写了一个简单的pexpect代码,但它与提示符不匹配并挂起。
这是我的代码
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')
这是另一个与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
,因为后面的方法需要一个正则表达式,其中[
是一个特殊字符。
问题是你的\\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')
保持client.py
就是这样(不要在那里添加\\r
到raw_input
字符串)。
你可以在Anything
方面做一些Anything
,然后继续你的代码。
child.pexpect('Anything')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.