20

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.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Jack Ryan
  • 8,396
  • 4
  • 37
  • 76

2 Answers2

16

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
FLAK-ZOSO
  • 3,873
  • 4
  • 8
  • 28
Gregg Lind
  • 20,690
  • 15
  • 67
  • 81
11

Does PExpect fits your needs?

elder_george
  • 7,849
  • 24
  • 31