21

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 operating system. Pipe anything from standard input to the CLI application, and then pipe any output from the CLI application to standard output.

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 technique is called. I have no idea what I need to be searching for.

2 Answers 2

17

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
Sign up to request clarification or add additional context in comments.

2 Comments

+1 since subprocess library is part of Python Standard Library, and gets the job done.
More than just that, with Popen you can use a list of arguments as args !
11

Does PExpect fits your needs?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.