测量服务器的 ping 延迟 - Python [英] Measuring ping latency of a server - Python

查看:31
本文介绍了测量服务器的 ping 延迟 - Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个服务器IP地址列表,我需要检查每个服务器是否在线以及延迟是多长时间.

我还没有找到任何直接的方法来实现这一点,而且在准确计算延迟方面似乎存在一些问题.

<小时>

有什么想法吗?

解决方案

如果你已经习惯了解析字符串,你可以使用 subprocess 模块将你要查找的数据变成一个字符串,就像这样:

<预><代码>>>>导入子流程>>>p = subprocess.Popen(["ping.exe","www.google.com"], stdout = subprocess.PIPE)>>>打印 p.communicate()[0]用 32 字节的数据 Ping www.l.google.com [209.85.225.99]:来自 209.85.225.99 的回复:bytes=32 time=59ms TTL=52来自 209.85.225.99 的回复:bytes=32 time=64ms TTL=52来自 209.85.225.99 的回复:bytes=32 time=104ms TTL=52来自 209.85.225.99 的回复:bytes=32 time=64ms TTL=52209.85.225.99 的 Ping 统计数据:数据包:发送 = 4,接收 = 4,丢失 = 0(0% 丢失),大约以毫秒为单位的往返时间:最小值 = 59 毫秒,最大值 = 104 毫秒,平均值 = 72 毫秒

I have a list of server IP addresses, I need to check if each one is online and how long the latency is.

I haven't found any straight forward ways of implementing this, and there seems to be a few problems in calculating latency accurately.


Any ideas?

解决方案

If you are already comfortable with parsing strings, you can use the subprocess module to get the data you are looking for into a string, like this:

>>> import subprocess
>>> p = subprocess.Popen(["ping.exe","www.google.com"], stdout = subprocess.PIPE)
>>> print p.communicate()[0]

Pinging www.l.google.com [209.85.225.99] with 32 bytes of data:

Reply from 209.85.225.99: bytes=32 time=59ms TTL=52
Reply from 209.85.225.99: bytes=32 time=64ms TTL=52
Reply from 209.85.225.99: bytes=32 time=104ms TTL=52
Reply from 209.85.225.99: bytes=32 time=64ms TTL=52

Ping statistics for 209.85.225.99:
    Packets: Sent = 4, Received = 4, Lost = 0 (0% loss),
Approximate round trip times in milli-seconds:
    Minimum = 59ms, Maximum = 104ms, Average = 72ms

这篇关于测量服务器的 ping 延迟 - Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆