views:

272

answers:

2

I am interested in controlling an interactive CLI application from python calls.

I guess at the most basic level I need a python script that will start a CLI application on the host OS. Pipe anything from stdin to the cli application, and then pipe any output from the cli application to stdout.

From this base It should be pretty straightforward to do some processing on the input and output

To be honest I probably just need a pointer on what the tecnique is called. I have no idea what I need to be searching for.

A: 

Does PExpect fits your needs?

elder_george
+2  A: 

Maybe you want something from Subprocess (MOTW).

I use code like this to make calls out to the 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
Gregg Lind
+1 since `subprocess` library is part of Python Standard Library, and gets the job done.
Pavel Repin
More than just that, with Popen you can use a list of arguments as args !
edomaur