网络质量
TCP 握手延迟测试
建立纯 TCP 连接,记录握手时间后立即断开,不涉及应用层协议,适合测试游戏网关、数据库等 4 层业务的网络延迟。
python3 -c "
import socket
import time
host = 'api.example.com'
port = 3250
print(f'正在测试 {host}:{port} 的 TCP 握手延迟...')
while True:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2) # 设置2秒超时
start = time.time()
s.connect((host, port))
latency = (time.time() - start) * 1000
print(f'连接成功: 延迟 {latency:.2f} ms')
s.close()
except Exception as e:
print(f'连接失败: {e}')
time.sleep(1)
"
预期输出
正在测试 api.example.com:3250 的 TCP 握手延迟...
连接成功: 延迟 45.12 ms
连接成功: 延迟 44.80 ms
连接成功: 延迟 46.01 ms
...
留言