如何通过python中的url / port来平服务器,并以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'}发布于 2021-03-28 01:50:55
您不能使用ping端口,因为Ping使用的是ICMP,它没有端口的概念。端口属于传输层协议,如TCP和UDP。
如果您希望进行只能通过端口访问的web服务器测试,则可以使用模块tcp_latency。
TCP延迟提供了一种使用TCP测量延迟的简单方法.
受其他类似工具的启发,tcp延迟来自使用Python在无服务器基础设施上运行网络诊断/故障排除任务的需要(因为许多提供程序不包括ping/ICMP支持),但是也应该在任何其他环境中使用Python>=36。
特性
作为命令行工具或在您的代码中作为一个模块运行。端口、运行、超时和运行之间的等待时间的自定义参数。IPv4 (例如52.26.14.11)和域(例如google.com)主机支持。当作为命令行工具运行时,人类可读的输出。没有外部依赖。体积小,可扩展。
pip3 install tcp_latency
python3现在您在python env中。
>>> 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.25, 409.26]其他部分:
如果你试着到处做平测试。我建议使用成熟的工具。Ping测试结果包括国家和国际客户的ping结果。通过谷歌搜索“全球平测”,排名第一的包括"https://tools.keycdn.com/ping","https://www.dotcom-tools.com/ping-test“等。
https://stackoverflow.com/questions/66830425
复制相似问题