在python脚本中包装交互式命令行应用程序

Jac*_*yan 16 python command-line

我对从python调用控制交互式CLI应用程序感兴趣.

我想在最基本的层面上我需要一个python脚本来启动主机操作系统上的CLI应用程序.管道从stdin到cli应用程序的任何内容,然后将cli应用程序的任何输出传递给stdout.

从这个基础对输入和输出进行一些处理应该非常简单

说实话,我可能只需要一个关于tecnique被调用的指针.我不知道我需要搜索什么.

Gre*_*ind 12

也许你想从Subprocess(MOTW)获得一些东西 .

我使用这样的代码来调用shell:

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


eld*_*rge 6

PExpect是否符合您的需求?