- 论坛徽章:
- 4
|
poll就别用了, 有延迟, 直接根据readline返回EOF做判定, 最后记得wait回收子进程, 否则有僵尸.- [root@vps616 python]# python main.py
- out-->PING www.google.cn (203.208.46.212) 56(84) bytes of data.<
- out-->64 bytes from 203.208.46.212: icmp_seq=1 ttl=54 time=2.41 ms<
- out-->64 bytes from 203.208.46.212: icmp_seq=2 ttl=54 time=2.06 ms<
- out-->64 bytes from 203.208.46.212: icmp_seq=3 ttl=54 time=2.14 ms<
- out-->64 bytes from 203.208.46.212: icmp_seq=4 ttl=54 time=2.65 ms<
- out--><
- out-->--- www.google.cn ping statistics ---<
- out-->4 packets transmitted, 4 received, 0% packet loss, time 3007ms<
- out-->rtt min/avg/max/mdev = 2.064/2.318/2.658/0.237 ms<
- DONE
- [root@vps616 python]# cat main.py
- #python2.7.3
- #coding=utf-8
- import subprocess
- pp = subprocess.Popen('ping -c4 www.google.cn', shell = True, stdout = subprocess.PIPE, stderr = subprocess.STDOUT)
- while True:
- line = pp.stdout.readline()
- if not line:
- break
- print('out-->' + line.strip() + '<')
- pp.wait()
- print('DONE')
复制代码 |
|