I have a linux application that receives input from some device. This input should be directed to the shell process so that it emulates a standard shell to the user. So far, I have done this by creating a process that runs '/ bin / sh', and I have redirected its input, output and stderr as follows:
import subprocess p = subprocess.Popen(shell=False, args=['/bin/sh'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) _make_nonblocking(p.stdout)
When I just pass the command, everything works:
p.stdin.write('pwd\n') p.stdout.read() '/home/dave\n'
For automatic completion, I tried to write:
p.stdin.write('ls s\t') p.stdout.read() IOError: [Errno 11] Resource temporarily unavailable
I expect to get a list of possible completions, but nothing will happen until I put '\ n' in stdin. (Also, there was nothing in stderr).
I looked at the telnetd code and saw the use of pty. I tried using pty.openpty () and setting slave as stdin, but that didn't work either. How to do it?
UPDATE: I used the -i option, as suggested. Now I have a problem that as soon as I use Popen and press ENTER, the python shell moves to the background as follows:
>>> p = subprocess.Popen(shell=False, args=['/bin/sh', '-i'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) >>> <ENTER> [1]+ Stopped ipython $