包裹在一个Python脚本的交互式命令行应用程序(Wrapping an interactive c

2019-08-18 02:45发布

我感兴趣的控制从Python会自动调用一个交互式CLI应用程序。

我想在最基本的层面上,我需要一个python脚本,将启动主机操作系统上的CLI应用程序。 从标准到CLI应用程序,然后通过管道从CLI应用到stdout任何输出管什么。

从这个基地应该是相当简单的做输入和输出部分处理

说实话,我可能只是需要什么tecn​​ique被称为指针。 我不知道我需要被搜索。

Answer 1:

不Pexpect的满足您的需求?



Answer 2:

也许你想从一些子过程 ( MOTW )。

我用这样的代码来拨打电话出去的外壳:

from subprocess import Popen, PIPE

## shell out, prompt
def shell(args, input=''):
    ''' uses subprocess pipes to call out to the shell.

    args:  args to the command
    input:  stdin

    returns stdout, stderr
    '''
    p = Popen(args, stdin=PIPE, stdout=PIPE, stderr=PIPE)
    stdout, stderr = p.communicate(input=input)
    return stdout, stderr


文章来源: Wrapping an interactive command line application in a python script