1 回答

TA贡献1817条经验 获得超6个赞
要显示子进程调用的输出,您可以使用check_output方法:有关详细信息,请参阅此答案
import subprocess
def ping():
print('Ping www.microsoft.com')
print()
address = 'www.microsoft.com'
print(subprocess.check_output(['ping', '-c', '3', address]).decode())
ping()
输出:
Ping www.microsoft.com
PING e13678.dspb.akamaiedge.net (23.53.160.151) 56(84) bytes of data.
64 bytes from a23-53-160-151.deploy.static.akamaitechnologies.com (23.53.160.151): icmp_seq=1 ttl=55 time=83.6 ms
64 bytes from a23-53-160-151.deploy.static.akamaitechnologies.com (23.53.160.151): icmp_seq=2 ttl=55 time=83.5 ms
64 bytes from a23-53-160-151.deploy.static.akamaitechnologies.com (23.53.160.151): icmp_seq=3 ttl=55 time=83.7 ms
--- e13678.dspb.akamaiedge.net ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 83.567/83.648/83.732/0.067 ms
添加回答
举报