Python Forum

Full Version: How to use pexpect in python?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I would like to control a robot using pexpect. Here is my code:

#!/usr/bin/env python

from pexpect import pxssh
import pexpect

s = pxssh.pxssh()
s.login('ubiquityrobot.local', 'ubuntu', 'ubuntu')
print 'Start'
s.sendline('rosrun simple_nav control.py')
s.prompt()
print(s.before)
s.expect('Load Path Name?', timeout=5)
s.sendline('yes')
s.prompt()
print(s.before)
s.expect('Enter the name of the Path:', timeout=5)
s.sendline('4')
s.prompt()
print(s.before)
s.expect('Load Path Name? ')
s.sendline('no')
s.prompt()
print(s.before)
s.logout()
The script is working fine till the end. Finally the script is closing with an error related with the timeout and eof. How to resolve this error? Please, help!
Maybe the following can help
https://pexpect.readthedocs.io/en/stable...t-patterns Wrote:Special EOF and TIMEOUT patterns
There are two special patterns to match the End Of File (EOF) or a Timeout condition (TIMEOUT). You can pass these patterns to expect(). These patterns are not regular expressions. Use them like predefined constants.

If the child has died and you have read all the child’s output then ordinarily expect() will raise an EOF exception. You can read everything up to the EOF without generating an exception by using the EOF pattern expect. In this case everything the child has output will be available in the before property.
...
... (see the link for more)