测量服务器的 ping 延迟 - Python
我有一个服务器IP 地址列表,我需要检查每个服务器是否在线以及延迟有多长。
我还没有找到任何直接的方法来实现这一点,并且在准确计算延迟方面似乎存在一些问题。
有什么想法吗?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
如果您已经熟悉解析字符串,则可以使用 subprocess 模块将您要查找的数据获取到字符串中,如下所示:
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:
遵循hlovdal的建议与fping,这是我用于测试代理的解决方案。我只在Linux下尝试过。如果无法测量 ping 时间,则会返回一个很大的值。用法:
print get_ping_time(':')
。Following hlovdal's suggestion to work with fping, here is my solution that I use for testing proxies. I only tried it under Linux. If no ping time could be measured, a big value is returned. Usage:
print get_ping_time('<ip>:<port>')
.这篇文章有点旧了,我认为今天存在更好的方法。我是 python 新手,但这是我在项目中所做的:
结果:
您可以在这里找到 pythonping 库: https ://pypi.org/project/pythonping/
This post is a bit old and I think better ways exists today. I'm new to python but here's what I did on my project:
Result:
You can find the pythonping library here: https://pypi.org/project/pythonping/
如果您想避免实现所有网络通信细节,您可以尝试在 fping 之上构建一些东西:
If you want to avoid implementing all the network communication details you could probably try to build something on top of fping:
https://github.com/matthieu-lapeyre/network-benchmark 基于我的解决方案关于 FlipperPA 的工作: https://github.com/FlipperPA/latency-tester
Github存储库:
https://github.com/matthieu-lapeyre/network-benchmark
https://github.com/matthieu-lapeyre/network-benchmark My solution based on the work of FlipperPA: https://github.com/FlipperPA/latency-tester
Github repository:
https://github.com/matthieu-lapeyre/network-benchmark
感谢贾巴,但该代码对我来说无法正常工作,所以我更改了如下内容
thanks from Jabba but that code doesn't work correctly for me so i change something like following