如何在单独的进程中运行shell并获得自动完成?(蟒蛇)

Dav*_*nes 2 python linux shell autocomplete

我有一个linux应用程序,从某些设备获取输入流.该输入应该定向到shell进程,以便它向用户模拟标准shell.到目前为止,我已经通过创建一个运行'/ bin/sh'的进程完成了它,我重定向了它的输入,输出和stderr,如下所示:

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)
Run Code Online (Sandbox Code Playgroud)

当我只是一个传递命令时,一切正常:

p.stdin.write('pwd\n')
p.stdout.read()
'/home/dave\n'
Run Code Online (Sandbox Code Playgroud)

为了自动完成,我尝试写:

p.stdin.write('ls s\t')
p.stdout.read()
IOError: [Errno 11] Resource temporarily unavailable
Run Code Online (Sandbox Code Playgroud)

我希望得到一个可能的完成列表,但是直到我把'\n'放在stdin中才会发生任何事情.(此外,斯特德尔没有等待).

我查看了telnetd代码并看到了pty的使用.我试图使用pty.openpty()并将slave设置为stdin,但这也不起作用.应该怎么做?

更新:我按照建议使用了-i参数.现在我有一个问题,一旦我使用Popen并按ENTER键,python shell移动到后台,如下所示:

>>> p = subprocess.Popen(shell=False, args=['/bin/sh', '-i'], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> <ENTER>
[1]+ Stopped                ipython
$ 
Run Code Online (Sandbox Code Playgroud)

Kar*_*ath 5

使用bash自动完成功能仅适用于交互模式:

-i        If the -i option is present, the shell is interactive.
Run Code Online (Sandbox Code Playgroud)

这将进行适当的仿真,包括显示提示和所有常用的东西.