Skip to content

Instantly share code, notes, and snippets.

@pitrou
Created April 25, 2017 18:54
Show Gist options
  • Select an option

  • Save pitrou/0c3586abb17d6a00853509e0afda9a68 to your computer and use it in GitHub Desktop.

Select an option

Save pitrou/0c3586abb17d6a00853509e0afda9a68 to your computer and use it in GitHub Desktop.
import socket
import ssl
import threading
import time
cert = 'Lib/test/keycert.pem'
ctx = ssl.create_default_context(cafile=cert)
ctx.check_hostname = False
#ctx.verify_mode = ssl.CERT_NONE
ctx.load_cert_chain(cert)
N_RUNS = 10
N_BYTES = 1024 * 1024 * 500
clock = time.monotonic
def run_serv(serv):
sock, addr = serv.accept()
sock = ctx.wrap_socket(sock, server_side=True)
buf = bytearray(N_BYTES // 2)
for i in range(N_RUNS):
total = 0
while total < N_BYTES:
total += sock.recv_into(buf)
#print(total)
sock.send(b".")
sock.close()
def run_cli(addr):
sock = socket.create_connection(addr)
sock = ctx.wrap_socket(sock, server_side=False)
print("cipher:", sock.cipher())
data = memoryview(b"x" * (N_BYTES // 2))
for i in range(N_RUNS):
t1 = clock()
remaining = N_BYTES
while remaining > 0:
remaining -= sock.send(data[:remaining])
dot = sock.recv(1)
t2 = clock()
assert dot == b'.'
print("%d bytes in %.3f s. => %d MB/s."
% (N_BYTES, t2 - t1, N_BYTES / 1e6 / (t2 - t1)))
sock.close()
def run():
serv = socket.socket()
serv.bind(('127.0.0.1', 0))
serv.listen(5)
t = threading.Thread(target=run_serv, args=(serv,), daemon=True)
t.start()
run_cli(serv.getsockname())
t.join()
if __name__ == "__main__":
run()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment