以前我一直用os.system()
處理一些系統(tǒng)管理任務,因為我認為那是運行l(wèi)inux命令最簡單的方式.</br>
我們能從Python官方文檔里讀到應該用subprocess
模塊來運行系統(tǒng)命令.subprocess
模塊允許我們創(chuàng)建子進程,連接他們的輸入/輸出/錯誤管道百新,還有獲得返回值。
subprocess
模塊打算來替代幾個過時的模塊和函數(shù)诵竭,比如:os.system, os.spawn*, os.popen*, popen2.*
命令蜓谋。</br>
讓我們來看一下subprocess
有哪些不同的函數(shù).</br>
subprocess.call()
執(zhí)行由參數(shù)提供的命令.</br>
我們可以用數(shù)組作為參數(shù)運行命令,也可以用字符串作為參數(shù)運行命令(通過設置參數(shù)shell=True
)</br>
注意,參數(shù)shell
默認為False
我們用subprocess.call()
來做一個統(tǒng)計磁盤的例子:
subprocess.call(['df', '-h'])
下面的例子把shell
設置為True
subprocess.call('du -hs $HOME', shell=True)
注意,python官方文檔里對參數(shù)shell=True
陳述了一個警告:
Invoking the system shell with shell=True can be a security hazard if combined
with untrusted input
現(xiàn)在,我們來看看輸入與輸出</br>
Input and Output
subprocess
模塊能阻止輸出,當你不關心標準輸出的時候是非常方便的.</br>
它也使你通過一種正確的方式管理輸入/輸出,有條理地整合python腳本中的的shell命令.
Return Codes
通過subprocess.call
的返回值你能夠判定命令是否執(zhí)行成功.</br>
每一個進程退出時都會返回一個狀態(tài)碼学密,你可以根據(jù)這個狀態(tài)碼寫一些代碼弥雹。
stdin, stdout and stderr
我在使用subprocess
時,有一個微妙的部分是怎么使用管道把命令連接起來.</br>
管道表明一個新的子管道應該被創(chuàng)建.</br>
默認的設置為None
,意味著沒有重定向發(fā)生</br>
標準錯誤可以指向標準輸出,表明子進程的錯誤信息會被捕獲到和標準輸出同一個文件.
subprocess.Popen()
subprocess
模塊中基本的進程創(chuàng)建和管理由Popen
類來處理.
subprocess.popen
是用來替代os.popen
的.</br>
我們來做一些真實的例子,subprocess.Popen
需要一個數(shù)組作為參數(shù):
import subprocess
p = subprocess.Popen(["echo", "hello world"], stdout=subprocess.PIPE)
print p.communicate()
>>>('hello world
', None)
注意,雖然你可以使用 "shell=True",但并不推薦這樣的方式.</br>
如果你知道你只用幾個有限的函數(shù),比如Popen
和PIPE
,你可以單單指定這幾個函數(shù):
from subprocess import Popen, PIPE
p1 = Popen(["dmesg"], stdout=PIPE)
print p1.communicate()
Popen.communicate()
communicate()
函數(shù)返回一個tuple
(標準輸出和錯誤).</br>
Popen.communicate()
和進程溝通:發(fā)送數(shù)據(jù)到標準輸入.從標準輸出和錯誤讀取數(shù)據(jù)直到遇到結束符.等待進程結束.</br>
輸入?yún)?shù)應該是一個字符串,以傳遞給子進程,如果沒有數(shù)據(jù)的話應該是None
.</br>
基本上,當你用communicate()
函數(shù)的時候意味著你要執(zhí)行命令了.
用subprocess
寫Ping
程序
我們先問用戶地址,然后用ping
請求這個地址.</br>
# Import the module
import subprocess
# Ask the user for input
host = raw_input("Enter a host to ping: ")
# Set up the echo command and direct the output to a pipe
p1 = subprocess.Popen(['ping', '-c 2', host], stdout=subprocess.PIPE)
# Run the command
output = p1.communicate()[0]
print output