python实现ping
时间: 2023-11-12 22:07:19 浏览: 197
Python实现ping命令
可以使用Python中的subprocess模块来实现ping命令。具体代码如下:
```python
import subprocess
hostname = "www.baidu.com"
response = subprocess.getoutput(f"ping -c 1 {hostname}")
print(response)
```
输出结果类似于:
```
PING www.a.shifen.com (220.181.38.148) 56(84) bytes of data.
64 bytes from 220.181.38.148 (220.181.38.148): icmp_seq=1 ttl=57 time=16.3 ms
--- www.a.shifen.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 16.382/16.382/16.382/0.000 ms
```
阅读全文