標籤:ISE 檢查 trace close end sleep /etc/ 開始 out
subprocess模組是python從2.4版本開始引入的模組。主要用來取代 一些舊的模組方法,如os.system、os.spawn*、os.popen*、commands.*等。subprocess通過子進程來執行外部指令,並通過input/output/error管道,擷取子進程的執行的返回資訊。
常用方法:
subprocess.call():執行命令,並返回執行狀態,其中shell參數為False時,命令需要通過列表的方式傳入,當shell為True時,可直接傳入命令
樣本如下:
>>> a = subprocess.call([‘df‘,‘-hT‘],shell=False)Filesystem Type Size Used Avail Use% Mounted on/dev/sda2 ext4 94G 64G 26G 72% /tmpfs tmpfs 2.8G 0 2.8G 0% /dev/shm/dev/sda1 ext4 976M 56M 853M 7% /boot>>> a = subprocess.call(‘df -hT‘,shell=True)Filesystem Type Size Used Avail Use% Mounted on/dev/sda2 ext4 94G 64G 26G 72% /tmpfs tmpfs 2.8G 0 2.8G 0% /dev/shm/dev/sda1 ext4 976M 56M 853M 7% /boot
>>> print a
0
subprocess.check_call():用法與subprocess.call()類似,區別是,當傳回值不為0時,直接拋出異常
樣本:
>>> a = subprocess.check_call(‘df -hT‘,shell=True)Filesystem Type Size Used Avail Use% Mounted on/dev/sda2 ext4 94G 64G 26G 72% /tmpfs tmpfs 2.8G 0 2.8G 0% /dev/shm/dev/sda1 ext4 976M 56M 853M 7% /boot>>> print a0>>> a = subprocess.check_call(‘dfdsf‘,shell=True)/bin/sh: dfdsf: command not foundTraceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/lib64/python2.6/subprocess.py", line 502, in check_call raise CalledProcessError(retcode, cmd)subprocess.CalledProcessError: Command ‘dfdsf‘ returned non-zero exit status 127
subprocess.check_output():用法與上面兩個方法類似,區別是,如果當傳回值為0時,直接返回輸出結果,如果傳回值不為0,直接拋出異常。需要說明的是,該方法在python3.x中才有。
subprocess.Popen():
在一些複雜情境中,我們需要將一個進程的執行輸出作為另一個進程的輸入。在另一些情境中,我們需要先進入到某個輸入環境,然後再執行一系列的指令等。這個時候我們就需要使用到suprocess的Popen()方法。該方法有以下參數:
args:shell命令,可以是字串,或者序列類型,如list,tuple。
bufsize:緩衝區大小,可不用關心
stdin,stdout,stderr:分別表示程式的標準輸入,標準輸出及標準錯誤
shell:與上面方法中用法相同
cwd:用於設定子進程的目前的目錄
env:用於指定子進程的環境變數。如果env=None,則預設從父進程繼承環境變數
universal_newlines:不同系統的的分行符號不同,當該參數設定為true時,則表示使用\n作為分行符號
樣本1,在/root下建立一個suprocesstest的目錄:
>>> a = subprocess.Popen(‘mkdir subprocesstest‘,shell=True,cwd=‘/root‘)
樣本2,使用python執行幾個命令:
import subprocessobj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)obj.stdin.write(‘print 1 \n‘)obj.stdin.write(‘print 2 \n‘)obj.stdin.write(‘print 3 \n‘)obj.stdin.write(‘print 4 \n‘)obj.stdin.close()cmd_out = obj.stdout.read()obj.stdout.close()cmd_error = obj.stderr.read()obj.stderr.close()print cmd_outprint cmd_error
也可以使用如下方法:
import subprocessobj = subprocess.Popen(["python"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)obj.stdin.write(‘print 1 \n‘)obj.stdin.write(‘print 2 \n‘)obj.stdin.write(‘print 3 \n‘)obj.stdin.write(‘print 4 \n‘)out_error_list = obj.communicate()print out_error_list
樣本3,將一個子進程的輸出,作為另一個子進程的輸入:
import subprocesschild1 = subprocess.Popen(["cat","/etc/passwd"], stdout=subprocess.PIPE)child2 = subprocess.Popen(["grep","0:0"],stdin=child1.stdout, stdout=subprocess.PIPE)out = child2.communicate()
其他方法:
import subprocesschild = subprocess.Popen(‘sleep 60‘,shell=True,stdout=subprocess.PIPE)child.poll() #檢查子進程狀態child.kill() #終止子進程child.send_signal() #向子進程發送訊號child.terminate() #終止子進程
python的subprocess模組