summaryrefslogtreecommitdiffstats
path: root/test/functional/util/dummy_ssl.py
blob: e150b151a03684afba589255b3b5a5677429c640 (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
67
68
69
70
71
72
#!/usr/bin/env python

import SocketServer
import dummy_killer

import time
import os
import sys
import socket
import ssl

PORT = 14433
HOST_NAME = '127.0.0.1'

PID = "/tmp/dummy_ssl.pid"

class SSLTCPHandler(SocketServer.BaseRequestHandler):
    def handle(self):
        time.sleep(0.5)
        data = self.request.recv(16384)
        while data:
            print "{} wrote:".format(self.client_address[0])
            print data
            time.sleep(0.1)
            self.request.sendall(data)
            time.sleep(0.1)
            data = self.request.recv(16384)

class SSL_TCP_Server(SocketServer.TCPServer):
    def __init__(self,
                 server_address,
                 RequestHandlerClass,
                 certfile,
                 keyfile,
                 ssl_version=ssl.PROTOCOL_TLSv1,
                 bind_and_activate=True):
        self.allow_reuse_address = True
        SocketServer.TCPServer.__init__(self, server_address,
                                        RequestHandlerClass, bind_and_activate)
        self.certfile = certfile
        self.keyfile = keyfile
        self.ssl_version = ssl_version
        #self.timeout = 1

    def get_request(self):
        newsocket, fromaddr = self.socket.accept()
        connstream = ssl.wrap_socket(newsocket,
                                     server_side=True,
                                     certfile = self.certfile,
                                     keyfile = self.keyfile,
                                     ssl_version = self.ssl_version)
        return connstream, fromaddr

    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)

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

class SSL_ThreadingTCPServer(SocketServer.ThreadingMixIn, SSL_TCP_Server): pass

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