2009-10-14 76 views
15

我有興趣從python調用中控制交互式CLI應用程序。在Python腳本中包裝交互式命令行應用程序

我想在最基本的層面上,我需要一個python腳本來啓動主機操作系統上的CLI應用程序。將標準輸入到cli應用程序中的任何內容,然後將cli應用程序的輸出管道輸出到標準輸出。

從這個基地應該是相當簡單的做輸入和輸出部分處理

說實話,我可能只是需要什麼tecn​​ique被稱爲指針。我不知道我需要尋找什麼。

回答

5

請問PExpect是否符合您的需求?

11

也許你想要的東西從SubprocessMOTW)。

我用這樣的代碼來撥打電話出去外殼:

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 
+0

+1因爲'subprocess'庫是Python標準庫的一部分,就完事了。 – 2009-10-15 05:56:11

+0

不僅僅如此,在Popen中,您可以使用參數列表作爲參數! – edomaur 2009-10-15 15:10:33

相關問題