/
tls_client.py
55 lines (42 loc) · 1.27 KB
/
tls_client.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#!/usr/bin/env python3
import socket
import ssl
from random import randrange
from multiprocessing import Process
HOST = '127.0.0.1'
PORT = 5004
WORKERS = 500
ITERATIONS_PER_WORKER = 1000
SOCKET_TIMEOUT = 5
def worker():
sslctx = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
sslctx.check_hostname = False
sslctx.verify_mode = ssl.CERT_NONE
sock = socket.create_connection((HOST, PORT))
ssock = sslctx.wrap_socket(sock)
ssock.settimeout(SOCKET_TIMEOUT)
for i in range(0, ITERATIONS_PER_WORKER):
msg = random_message()
ssock.send(msg)
# Read our message back. Never send more than one message at a time per
# connection.
buf = ssock.read(len(msg))
if buf == msg:
print("Server successfully echoed %d byte message" % (len(msg)))
else:
print("Wrong response for %d byte message" % (len(msg)))
ssock.shutdown(socket.SHUT_RDWR)
sock.close()
def random_message():
arr = []
for i in range(0, randrange(16, 512)):
arr.append(randrange(0, 255))
return bytes(arr)
if __name__ == '__main__':
workers = []
for i in range(0, WORKERS):
workers.append(Process(target=worker))
for w in workers:
w.start()
for w in workers:
w.join()