[TOC]
<br>
### subprocess 模塊
Python引入subprocess模塊來管理子進程,以取代一些舊模塊的方法:如 os.system、os.spawn*、os.popen*、popen2.*、commands.*不但可以調用外部的命令作為子進程,而且可以連接到子進程的input/output/error管道,獲取相關的返回信息。
#### subprocess.call(*popenargs, timeout=None, **kwargs)
執行命令,等待命令完成或超時,然后再返回狀態碼
```cmd
>>> subprocess.call("netstat -an | findstr 80",shell=True)
TCP 192.168.0.106:60838 121.41.82.44:80 TIME_WAIT
TCP 192.168.0.106:60839 121.41.82.44:80 TIME_WAIT
UDP 127.0.0.1:58045 *:*
UDP 127.0.0.1:58046 *:*
0
```
#### subprocess.Popen(...)
在一個新進程中執行子程序,具體參數請查看源碼
```cmd
>>> netstat = subprocess.Popen("netstat -an | findstr 80", shell=True,stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
>>> netstat
<subprocess.Popen object at 0x000002965A2EE780>
>>> netstat.stdout.read()
b' TCP 192.168.0.106:60865 52.230.85.180:443 ESTABLISHED\r\n TCP 192.168.0.106:60943 140.207.124.210:80 LAST_ACK\r\n TCP 192.168.0.106:60950 121.41.82.44:80 TIME_WAIT\r\n TCP 192.168.0.106:60951 121.41.82.44:80 TIME_WAIT\r\n UDP 127.0.0.1:58045 *:* \r\n UDP 127.0.0.1:58046 *:* \r\n'
>>> netstat.stderr.read()
b''
>>>
```
<hr style="margin-top:100px">
:-: 
***微信掃一掃,關注“python測試開發圈”,了解更多測試教程!***
- 前言
- chapter01_開發環境
- chapter02_字符串的使用
- chapter03_列表的使用
- chapter04_字典的使用
- chapter05_數字的使用
- chapter06_元組的使用
- chapter07_集合的使用
- chapter08_輸入輸出
- chapter09_控制流程
- chapter10_實例練習_登錄1
- chapter11_python函數入門
- chapter12_python中的類
- chapter13_輕松玩轉python中的模塊管理
- chapter14_掌握學習新模塊的技巧
- chapter15_通過os模塊與操作系統交互
- chapter16_子進程相關模塊(subprocess)
- chapter17_時間相關模塊(time & datetime)
- chapter18_序列化模塊(json)
- chapter19_加密模塊(hashlib)
- chapter20_文件的讀與寫
- chapter21_階段考核2_登錄
- chapter22_小小算法挑戰(排序&二分法)
- chapter23_用多線程來搞事!
- chapter24_HTTP接口請求(requests)
- chapter25_接口測試框架(pytest)
- chapter26_階段考核3_HTTP接口測試
- chapter27_HTML解析(pyquery)
- chapter28_階段考核4_爬蟲下載網易汽車
- chapter29_python中的那些編碼坑
- chapter30_MySQL數據庫操作
- chapter31 高級特性_迭代器與生成器
- chapter32 高級特性_裝飾器
- chapter33 高級特性_列表處理