如何解决如何ping服务器url/端口?
如何通过python中的url/端口ping服务器,并以ms(毫秒)为单位接收响应?
我打我的头,我用过tcping
,我正在研究套接字,但它看起来并不那么简单,有什么提示吗?
pings_ookla_script_root-OK.py
:
#!/usr/bin/env python3
import requests
import os
import sys
import pings
p = pings.Ping(quiet=False)
port = 8080
response = p.ping('google.com',times=4)
print(response)
输出:
47 bytes from 142.250.78.206: icmp_seq=0 ttl=114 time=11.326 ms
47 bytes from 142.250.78.206: icmp_seq=1 ttl=114 time=11.059 ms
47 bytes from 142.250.78.206: icmp_seq=2 ttl=114 time=10.793 ms
47 bytes from 142.250.78.206: icmp_seq=3 ttl=114 time=11.213 ms
--- google.com ping statistics ---
4 packets transmitted,4 packets received,0.0% packet loss
round-trip min/avg/max = 10.793/11.098/11.326 ms
{'max_rtt': 11.325597763061523,'min_rtt': 10.793447494506836,'avg_rtt': 11.09778881072998,'packet_lost': None,'ret_code': 0,'packet_size': 47,'timeout ': 1000,'dest': 'google.com','dest_ip': '142.250.78.206'}
解决方法
您无法 ping 端口,因为 Ping 使用的是没有端口概念的 ICMP。端口属于传输层协议,如 TCP 和 UDP。
如果你想做只能通过端口访问的网络服务器测试,你可以使用模块tcp_latency
tcp-latency 提供了一种使用 TCP 测量延迟的简单方法。
受其他类似工具的启发,tcp-latency 来自在无服务器基础设施上使用 Python 运行网络诊断/故障排除任务的需要(因为许多提供商不包括 ping/ICMP 支持),但也应该在任何其他使用 Python 的环境中工作>= 36.
特点
作为命令行工具运行或在您的代码中作为模块运行。
端口、运行、超时和运行之间的等待时间的自定义参数。
IPv4(例如 52.26.14.11)和域(例如 google.com)主机支持。
作为命令行工具运行时的人类可读输出。
没有外部依赖。
小巧且可扩展。
pip3 install tcp_latency
python3
现在你在 python 环境中。
>>> from tcp_latency import measure_latency
>>> measure_latency(host='google.com')
[34.57]
>>> measure_latency(host='52.26.14.11',port=80,runs=10,timeout=2.5)
[433.82,409.21,409.25,307.09,306.64,409.45,306.58,306.93,409.26]
其他问题:
如果您尝试在任何地方进行 ping 测试。我建议使用成熟的工具。 Ping 测试结果包括国内和国际客户端ping 结果。 谷歌搜索“全球ping测试”,排名靠前的包括“https://tools.keycdn.com/ping”、“https://www.dotcom-tools.com/ping-test”等>
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。