Skip to content

Instantly share code, notes, and snippets.

@pejotes
Created March 6, 2025 15:36
Show Gist options
  • Select an option

  • Save pejotes/77fc35b7da90026416fac39805269cd9 to your computer and use it in GitHub Desktop.

Select an option

Save pejotes/77fc35b7da90026416fac39805269cd9 to your computer and use it in GitHub Desktop.
measure_handshake.py
"measure_handshake.py" 39L, 1486B 39,64 All
import socket
import ssl
import time
import sys
import argparse
def measure_handshake(host, port, iterations=10):
times = []
for i in range(iterations):
try:
start = time.time()
context = ssl.create_default_context()
conn = context.wrap_socket(socket.socket(socket.AF_INET),
server_hostname=host)
conn.connect((host, port))
elapsed = time.time() - start
conn.close()
times.append(elapsed)
print(f"Handshake {i+1}: {elapsed:.4f}s")
except Exception as e:
print(f"Error in handshake {i+1}: {e}")
if times:
avg_time = sum(times) / len(times)
print(f"\nAverage handshake time: {avg_time:.4f}s")
print(f"Min: {min(times):.4f}s, Max: {max(times):.4f}s")
else:
print("No successful handshakes completed")
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Measure SSL handshake times")
parser.add_argument("hostname", help="The hostname to connect to")
parser.add_argument("-p", "--port", type=int, default=443, help="Port number (default: 443)")
parser.add_argument("-n", "--iterations", type=int, default=10, help="Number of handshakes to perform (default: 10)")
args = parser.parse_args()
print(f"Testing SSL handshake performance to {args.hostname}:{args.port}")
measure_handshake(args.hostname, args.port, args.iterations)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment