subprocess模塊
subprocess主要用於提供執行系統命令的功能,其在python3中將commands、os.system,os.popen等功能全部集成了,所以在py3中推薦使用該模塊
下面是可以執行shell命令的相關模塊和函數:
os.system
os.spawn*
os.popen* --廢棄
popen2.* --廢棄
commands.* --廢棄,3.x中被移除
常用方法
- subprocess.call() 執行命令,取出命令執行的結果,通常是shell返回的狀態碼,注意call會把執行結果打印出來,但是只會取執行結果的狀態碼
>>> import subprocess >>> res = subprocess.call('df -h',shell=True) Filesystem Size Used Avail Use% Mounted on /dev/vda3 19G 9.4G 8.2G 54% / tmpfs 3.9G 0 3.9G 0% /dev/shm /dev/vda1 194M 32M 153M 18% /boot >>> print(res) 0 >>> res = subprocess.call(['df', '-h'],shell=False) Filesystem Size Used Avail Use% Mounted on /dev/vda3 19G 9.4G 8.2G 54% / tmpfs 3.9G 0 3.9G 0% /dev/shm /dev/vda1 194M 32M 153M 18% /boot >>> print(res) 0 >>> res = subprocess.call('123',shell=True) /bin/sh: 123: command not found >>> print(res) 127
補充:
shell = True ,允許 shell 命令是字符串形式,否則,必須按列表形式傳參數
- subprocess.check_call() 執行命令,如果執行狀態碼是 0 ,則返回0,否則拋異常。注意,如果命令執行成功,則subprocess.check_call()為0,否則subprocess.check_call()1沒有值
>>> res1 = subprocess.check_call('free -m',shell=True) total used free shared buffers cached Mem: 7870 5217 2653 0 488 3108 -/+ buffers/cache: 1620 6250 Swap: 1023 0 1023 >>> print(res1) 0 >>> res2 = subprocess.check_call('123',shell=True) /bin/sh: 123: command not found Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/python3/lib/python3.5/subprocess.py", line 584, in check_call raise CalledProcessError(retcode, cmd) subprocess.CalledProcessError: Command '123' returned non-zero exit status 127 >>> print(res2) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'res2' is not defined
- check_output 執行命令,如果狀態碼是 0 ,則返回執行結果,否則拋異常.
>>> res = subprocess.check_output('df -h',shell=True) >>> print(res) b'Filesystem Size Used Avail Use% Mounted on\n/dev/vda3 19G 9.4G 8.2G 54% /\ntmpfs 3.9G 0 3.9G 0% /dev/shm\n/dev/vda1 194M 32M 153M 18% /boot\n' >>> res1 = subprocess.check_output('dfaa',shell=True) /bin/sh: dfaa: command not found Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/python3/lib/python3.5/subprocess.py", line 629, in check_output **kwargs).stdout File "/usr/local/python3/lib/python3.5/subprocess.py", line 711, in run output=stdout, stderr=stderr) subprocess.CalledProcessError: Command 'dfaa' returned non-zero exit status 127 >>> print(res1) Traceback (most recent call last): File "<stdin>", line 1, in <module> NameError: name 'res1' is not defined
-
subprocess.Popen 執行復雜的命令
- Popen類:
class Popen(args, bufsize=0, executable=None, stdin=None, stdout=None, stderr=None, preexec_fn=None, close_fds=False, shell=False, cwd=None, env=None, universal_newlines=False, startupinfo=None, creationflags=0)
-
參數說明:
- args:shell命令,可以是字符串或者序列類型(如:list,元組)
- bufsize:指定緩沖。0 無緩沖,1 行緩沖,其他 緩沖區大小,負值 系統緩沖
- stdin, stdout, stderr:分別表示程序的標准輸入、輸出、錯誤句柄
- preexec_fn:只在Unix平台下有效,用於指定一個可執行對象(callable object),它將在子進程運行之前被調用
- close_sfs:在windows平台下,如果close_fds被設置為True,則新創建的子進程將不會繼承父進程的輸入、輸出、錯誤管道。所以不能將close_fds設置為True同時重定向子進程的標准輸入、輸出與錯誤(stdin, stdout, stderr)。
- shell:同上
- cwd:用於設置子進程的當前目錄
- env:用於指定子進程的環境變量。如果env = None,子進程的環境變量將從父進程中繼承。
- universal_newlines:不同系統的換行符不同,True -> 同意使用 \n
-
startupinfo與createionflags只在windows下有效將被傳遞給底層的CreateProcess()函數,用於設置子進程的一些屬性,如:主窗口的外觀,進程的優先級等等
-
執行普通命令:
>>> child = subprocess.Popen(['ping','-c','4','proxy.fuzengjie.cn']) >>> PING proxy.fuzengjie.cn (23.83.227.252) 56(84) bytes of data. 64 bytes from proxy.fuzengjie.cn (23.83.227.252): icmp_seq=1 ttl=48 time=188 ms 64 bytes from proxy.fuzengjie.cn (23.83.227.252): icmp_seq=2 ttl=48 time=184 ms 64 bytes from proxy.fuzengjie.cn (23.83.227.252): icmp_seq=3 ttl=48 time=184 ms 64 bytes from proxy.fuzengjie.cn (23.83.227.252): icmp_seq=4 ttl=48 time=184 ms --- proxy.fuzengjie.cn ping statistics --- 4 packets transmitted, 4 received, 0% packet loss, time 3273ms rtt min/avg/max/mdev = 184.661/185.637/188.307/1.544 ms >>>
從運行結果中看到,父進程在開啟子進程之后並沒有等待child的完成,而是直接運行print
還可以獲取子進程的狀況
* child.poll() # 檢查子進程狀態
* child.kill() # 終止子進程
* child.send_signal() # 向子進程發送信號
* child.terminate() # 終止子進程
* child.pid #子進程的pid
- 管道
可以在Popen()建立子進程的時候改變標准輸入、標准輸出和標准錯誤,並可以利用subprocess.PIPE將多個子進程的輸入和輸出連接在一起,構成管道(pipe)
import subprocess obj = subprocess.Popen(["python3"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, universal_newlines=True) obj.stdin.write("print asd") obj.stdin.close() cmd_out = obj.stdout.read() obj.stdout.close() cmd_error = obj.stderr.read() obj.stderr.close() print(123) print(cmd_out) print(cmd_error) 輸出: 123 File "<stdin>", line 2 print asd ^ SyntaxError: Missing parentheses in call to 'print'
subprocess.PIPE實際上為文本流提供一個緩存區。stdin 將標准輸入放到緩沖區,系統執行完之后,會把標准輸出和標准錯誤輸出放到緩沖區,這樣直接可以從緩沖區里拿出結果
- 父子進程通信
>>> import subprocess >>> child1 = subprocess.Popen('netstat -lnpt',shell=True,stdout=subprocess.PIPE) >>> child2 = subprocess.Popen(["grep","80"],stdin=child1.stdout, stdout=subprocess.PIPE) >>> print(child2.stdout.read()) b'tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9804/nginx \n' >>> print(child2.stdout.read()) b'' >>> child1 = subprocess.Popen('netstat -lnpt',shell=True,stdout=subprocess.PIPE) >>> child2 = subprocess.Popen(["grep","80"],stdin=child1.stdout, stdout=subprocess.PIPE) >>> output = child2.communicate() >>> print(output) (b'tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9804/nginx \n', None) >>> print(output) (b'tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN 9804/nginx \n', None) >>> output = child2.communicate() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "/usr/local/python3/lib/python3.5/subprocess.py", line 1055, in communicate stdout = self.stdout.read() ValueError: read of closed file
child1的stdout將文本輸出到緩存區,隨后child2的stdin從該PIPE中將文本讀取走。child2的輸出文本也被存放在PIPE中,直到communicate()方法從PIPE中讀取出PIPE中的文本、注意:communicate()是Popen對象的一個方法,該方法會阻塞父進程,直到子進程完成。
