How to run a shell in a separate process and get auto completions? (Python)
Question: I have a linux application that gets an input stream from some device. That
input should be directed to a shell process so that it emulates to the user a
standard shell. So far, I've done it by creating a process that runs '/bin/sh'
and I 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) # sets O_NONBLOCK
    _make_nonblocking(p.stderr)
    

When I just a pass command, everything works:

    
    
    p.stdin.write('pwd\n')
    p.stdout.read()
    '/home/dave\n'
    

For auto completion, I tried writing:

    
    
    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 happens until I put
'\n' in stdin. (Also, there was nothing waiting in stderr).

I've looked through the telnetd code and saw the use of pty. I tried to use
pty.openpty() and set slave as stdin, but that didn't work either. How should
it be done?

UPDATE: I used the -i parameter as suggested. Now I have a problem that once I
use Popen and press ENTER the python shell move 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
    $ 
    


Answer: With `bash` autocompletion only works in interactive mode:

    
    
    -i        If the -i option is present, the shell is interactive.
    

This will do proper emulation, including displaying prompt and all the usual
stuff.

