學習日記之--subprocess模塊

python中執行shell命令的相關模塊和函數有:

os.system
os.spawn
os.popen --廢棄
popen2.* --廢棄
commands.* --廢棄,3.x中被移除

subprocess通過子進程來執行外部指令,並通過input/output/error管道,獲取子進程的執行的返回信息。

常用方法:

1, subprocess.call():執行命令,並返回執行狀態,其中shell參數爲False時,命令需要通過列表的方式傳入,當shell爲True時,可直接傳入命令

>>> a = subprocess.call(['df','-hT'], shell = False)

>>> a = subprocess.call('df -hT', shell = True)

>>> print a

0

2, subprocess.check_call():用法與subprocess.call()類似,區別是,當返回值不爲0時,直接拋出異常

#進入python執行python命令
obj = subprocess.Popen('python', shell = True, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
obj.stdin.write(b'print(1) \n')

#進入python環境後字符串必須爲bytes格式,不能是字符串,上面這種方式可以轉化,或者另外一種方式如下
str = 'print 2'
str = str.encode()
obj.stdin.write(str)
obj.stdin.close()

#進入shell環境執行shell命令
obj = subprocess.Popen('adb shell', shell = True,stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
str = 'cd data'
str = str.encode()
obj.stdin.write(str)
obj.stdin.write(b'./dumperLAHLOS.sh > /data/sram.txt & \n')
obj.stdin.write(b'exit \n')
obj.stdin.close()


#進入一些目錄去執行一些命令,比如我們要在D盤建立一個文件夾:
a = subprocess.Popen('mkdir aaaaa', shell=True, cwd='C:/')

3, subprocess.check_output():用法與上面兩個方法類似,區別是,如果當返回值爲0時,直接返回輸出結果,如果返回值不爲0,直接拋出異常。需要說明的是,該方法在python3.x中才有。

4, subprocess.Popen():

在一些複雜場景中,我們需要將一個進程的執行輸出作爲另一個進程的輸入。在另一些場景中,我們需要先進入到某個輸入環境,然後再執行一系列的指令等。這個時候我們就需要使用到suprocess的Popen()方法。該方法有以下參數:

args:shell命令,可以是字符串,或者序列類型,如list,tuple。

bufsize:緩衝區大小,可不用關心

stdin,stdout,stderr:分別表示程序的標準輸入,標準輸出及標準錯誤

shell:其中shell參數爲False時,命令需要通過列表的方式傳入,當shell爲True時,可直接傳入命令

cwd:用於設置子進程的當前目錄

env:用於指定子進程的環境變量。如果env=None,則默認從父進程繼承環境變量

universal_newlines:不同系統的的換行符不同,當該參數設定爲true時,則表示使用\n作爲換行符

示例1,在/root下創建一個suprocesstest的目錄:

>>> a = subprocess.Popen('mkdir subprocesstest',shell=True,cwd='/root')

示例2,使用python執行幾個命令:進入python環境後再執行一些python命令

import subprocess

obj = 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_out
print cmd_error

也可以使用如下方法:

import subprocess

obj = 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 subprocess
child1 = subprocess.Popen(["cat","/etc/passwd"], stdout=subprocess.PIPE)
child2 = subprocess.Popen(["grep","0:0"],stdin=child1.stdout, stdout=subprocess.PIPE)
out = child2.communicate()

其他方法:

import subprocess
child = subprocess.Popen('sleep 60',shell=True,stdout=subprocess.PIPE)
child.poll()    #檢查子進程狀態
child.kill()     #終止子進程
child.send_signal()    #向子進程發送信號
child.terminate()   #終止子進程
import subprocess
#進入python執行python命令
obj = subprocess.Popen('python', shell = True, stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
obj.stdin.write(b'print(1) \n')

#進入python環境後字符串必須爲bytes格式,不能是字符串,上面這種方式可以轉化,或者另外一種方式如下
str = 'print 2'
str = str.encode()
obj.stdin.write(str)
obj.stdin.close()

#進入shell環境執行shell命令
obj = subprocess.Popen('adb shell', shell = True,stdin = subprocess.PIPE, stdout = subprocess.PIPE, stderr = subprocess.PIPE )
str = 'cd data'
str = str.encode()
obj.stdin.write(str)
obj.stdin.write(b'./dumperLAHLOS.sh > /data/sram.txt & \n')
obj.stdin.write(b'exit \n')
obj.stdin.close()


#進入一些目錄去執行一些命令,比如我們要在D盤建立一個文件夾:
a = subprocess.Popen('mkdir aaaaa', shell=True, cwd='D:/')

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章