我们可以使用os.popen(cmd)来执行系统的命令并取到结果。
下面分别是Windows和Linux上的示例。
Windows:
>>> import os >>> print os.popen("ping www.qiansw.com -n 1").read() 正在 Ping dc3abcfc18d1eff8.cdn.fhldns.com [61.182.137.3] 具有 32 字节的数据: 来自 61.182.137.3 的回复: 字节=32 时间=14ms TTL=49 61.182.137.3 的 Ping 统计信息: 数据包: 已发送 = 1,已接收 = 1,丢失 = 0 (0% 丢失), 往返行程的估计时间(以毫秒为单位): 最短 = 14ms,最长 = 14ms,平均 = 14ms
Linux:
shell > python Python 2.6.6 (r266:84292, Feb 22 2013, 00:00:18) [GCC 4.4.7 20120313 (Red Hat 4.4.7-3)] on linux2 Type "help", "copyright", "credits" or "license" for more information. >>> import os >>> print os.popen("ping www.qiansw.com -c 1").read() PING dc3abcfc18d1eff8.cdn.fhldns.com (61.155.149.104) 56(84) bytes of data. 64 bytes from 61.155.149.104: icmp_seq=1 ttl=50 time=32.1 ms --- dc3abcfc18d1eff8.cdn.fhldns.com ping statistics --- 1 packets transmitted, 1 received, 0% packet loss, time 60ms rtt min/avg/max/mdev = 32.176/32.176/32.176/0.000 ms
这篇文章还没有人留言,快来抢沙发吧。