Network Quality
TCP handshake delay check
Establishes a TCP connection, records the handshake time, and immediately disconnects. It does not involve application layer protocols, making it suitable for testing network latency of Layer 4 services such as game gateways and databases.
python3 -c "
import socket
import time
host = 'api.example.com'
port = 3250
print(f'Testing TCP handshake latency for {host}:{port}...')
while True:
try:
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
s.settimeout(2) # Set 2-second timeout
start = time.time()
s.connect((host, port))
latency = (time.time() - start) * 1000
print(f'Connection successful: Latency {latency:.2f} ms')
s.close()
except Exception as e:
print(f'Connection failed: {e}')
time.sleep(1)
"
Expected output
Testing TCP handshake latency for {host}:{port}...
Connection successful: latency 45.12 ms
Connection successful: Latency 44.80 ms
Connection successful: Latency 46.01 ms
...
Comments