python ping3多线程ping测试
时间: 2023-11-07 22:50:42 浏览: 238
可以使用Python的ping3库和threading库来实现多线程ping测试。下面是一个简单的示例代码:
```python
import ping3
import threading
HOSTS = [
"www.google.com",
"www.baidu.com",
"www.github.com",
"www.stackoverflow.com"
]
class PingThread(threading.Thread):
def __init__(self, host):
threading.Thread.__init__(self)
self.host = host
def run(self):
response_time = ping3.ping(self.host)
if response_time is not None:
print(f"{self.host} is alive. Response time: {response_time} ms")
else:
print(f"{self.host} is down.")
if __name__ == "__main__":
threads = []
for host in HOSTS:
thread = PingThread(host)
threads.append(thread)
thread.start()
for thread in threads:
thread.join()
```
这个示例代码会创建多个PingThread线程来分别ping指定的主机,当收到响应时,会输出相应的信息。可以根据需要修改HOSTS列表中的主机名。
阅读全文