簡體   English   中英

在 Python 腳本中包裝交互式命令行應用程序

[英]Wrapping an interactive command line application in a Python script

我有興趣從 Python 調用中控制交互式 CLI 應用程序。

我想在最基本的層面上,我需要一個 Python 腳本,它將在主機操作系統上啟動一個 CLI 應用程序。 Pipe 從標准輸入到 CLI 應用程序的任何內容,然后是 pipe 任何 output 從 CLI 應用程序到標准 Z148E6281DDB863

在此基礎上,對輸入和 output 進行一些處理應該非常簡單。

老實說,我可能只需要一個關於該技術稱為什么的指針。 我不知道我需要尋找什么。

也許你想要來自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

PExpect是否符合您的需求?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM