aboutsummaryrefslogtreecommitdiffstats
path: root/test/functional/util/dummy_ssl.py
blob: 44b1782934f6792a8d9d92e8d1ffab59d57a304a (plain)
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
56
57
58
59
60
61
62
63
64
65
66
#!/usr/bin/env python3

import os
import socket
import ssl
import sys
import time

import dummy_killer
import socketserver

PORT = 14433
HOST_NAME = '127.0.0.1'

PID = "/tmp/dummy_ssl.pid"

class SSLTCPHandler(socketserver.StreamRequestHandler):
    def handle(self):
        time.sleep(0.5)
        data = self.request.recv(6000000)
        while data:
            print("{} wrote:".format(self.client_address[0]))
            print(data)
            time.sleep(0.1)
            self.request.sendall(b'hello\n')
            time.sleep(0.1)
            data = self.request.recv(6000000)

class SSL_TCP_Server(socketserver.ThreadingMixIn, socketserver.TCPServer):
    def __init__(self,
                 server_address,
                 RequestHandlerClass,
                 certfile,
                 keyfile,
                 bind_and_activate=True):
        self.allow_reuse_address = True
        super().__init__(server_address, RequestHandlerClass, False)
        self.timeout = 1
        ctx = ssl.create_default_context()
        ctx.load_cert_chain(certfile=certfile)
        ctx.check_hostname = False
        ctx.verify_mode = ssl.CERT_NONE
        self.socket = ctx.wrap_socket(self.socket, server_side=True)
        if (bind_and_activate):
            self.server_bind()
            self.server_activate()

    def run(self):
        dummy_killer.write_pid(PID)
        try:
            self.serve_forever()
        except KeyboardInterrupt:
            print("Interrupt")
        except socket.error as e:
            print("Socket closed {}".format(e))
        finally:
            self.server_close()

    def stop(self):
        self.keep_running = False
        self.server_close()

if __name__ == '__main__':
    server = SSL_TCP_Server((HOST_NAME, PORT), SSLTCPHandler, sys.argv[1], sys.argv[1])
    dummy_killer.setup_killer(server, server.stop)
    server.run()