subprocess
特点:
调用外部系统命令创建新的子进程
能够使用input/output/error
有返回值
可以替代旧的os.system,os.popen,commands
import subprocess
ping = subprocess.Popen(args="ping -c 4 )
print ping.pid
print ping.returncode
import subprocess
ping = subprocess.Popen(args="ping -c 4 )
print ping.pid
print ping.returncode
---------------------------------------------------------------------------------------------
使用wait()对进程进行控制,等待子进程执行完毕,在执行下面的语句
import subprocess
ping = subprocess.Popen(args="ping -c 4 )
ping.wait()
print ping.pid
print ping.returncode
PING (115.239.210.26) 56(84) bytes of data.
64 bytes from 115.239.210.26: icmp_req=1 ttl=127 time=32.0 ms
64 bytes from 115.239.210.26: icmp_req=2 ttl=127 time=33.9 ms
64 bytes from 115.239.210.26: icmp_req=3 ttl=127 time=31.7 ms
64 bytes from 115.239.210.26: icmp_req=4 ttl=127 time=31.0 ms
--- ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 25266ms
rtt min/avg/max/mdev = 31.013/32.192/33.903/1.061 ms
2754
0
--------------------------------------------------------------------------------------
stdout/stdin/stderr
import subprocess
ping = subprocess.Popen(args="ping -c 4 )
ping.wait()
print ping.stdout.read()
print ping.pid
print ping.returncode
---------------------------------------------------------------------------
阅读(1222) | 评论(0) | 转发(0) |