我想寫一個啓動子進程的python腳本,並寫入子進程標準輸入。對輸出進行一些測試,然後將更多命令寫入標準輸入。寫入標準輸入,訪問被拒絕
我曾嘗試:
def get_band():
print "band"
p = subprocess.Popen(["/path/to/program","-c","-"], stdout=subprocess.PIPE, stdin=subprocess.PIPE, stderr=subprocess.PIPE)
ran_stdout = p.communicate(input='show status')[0]
print(ran_stdout)
然而print語句給出:
Unable to connect at 127.0.0.1, Connection refused.
我在想,如果我這樣做對嗎?這裏是關於我試圖運行的過程的文檔。我想用最後一個選項。
Running the tool from the Linux shell allows additional options, depending on the options given to the command. The options are as follows:
-h Displays help about the command
-c <Filename> Instead of taking typed commands interactively from a user the commands are read from the named file, i.e. in batch mode. When all commands are processed the CLI session ends automatically.
-c - As above but reads command from Linux stdin. This allows commands to be ‘piped’ to the program.
消息是否可以由子進程生成? – Bogdacutu 2013-04-09 16:15:03
是的,謝謝你,我想知道爲什麼會這樣以及如何獲得許可?如果我使用這些參數從終端運行進程,它運行良好。 – Paul 2013-04-09 16:19:33