开发者

Wrapping an interactive command line application in a Python script

开发者 https://www.devze.com 2022-12-09 01:06 出处:网络
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开发者_如

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开发者_如何学Go 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.


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


Does PExpect fits your needs?

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号