Reputation: 563
I am trying to execute client.py from run.py. The client.py prompts for an input. I have written a simple pexpect code but it doesn't match the prompt and hangs.
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..
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'
>>>
Upvotes: 4
Views: 8848
Reputation: 174
You can just have Anything
in pexpect and then proceed with your code.
child.pexpect('Anything')
Upvotes: 0
Reputation: 502
The problem is your \n
character. 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
:
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).
Upvotes: 2
Reputation: 42758
You may want to use expect_exact
instead of expect
, because the later method expects an regular expression, where [
is a special character.
Upvotes: 4