1#!/usr/bin/python
2
3# HTTPS server test application.
4#
5# You can generate certificate file like this:
6#       openssl req -x509 -newkey rsa:2048 -keyout https-server.pem \
7#               -out https-server.pem -days 10000 -nodes \
8#               -subj '/CN=localhost'
9#
10# To see the contents of the certificate do this:
11#       openssl x509 -in https-server.pem  -text -noout
12#
13# To add the cert into your application do this:
14#       openssl x509 -in https-server.pem  -C -noout
15#
16
17import socket
18from BaseHTTPServer import HTTPServer
19from SimpleHTTPServer import SimpleHTTPRequestHandler
20import ssl
21
22PORT = 4443
23
24class HTTPServerV6(HTTPServer):
25    address_family = socket.AF_INET6
26
27class RequestHandler(SimpleHTTPRequestHandler):
28    length = 0
29
30    def _set_headers(self):
31        self.send_response(200)
32        self.send_header('Content-Type', 'text/html')
33        self.send_header('Content-Length', str(self.length))
34        self.end_headers()
35
36    def do_POST(self):
37        payload = "<html><p>Done</p></html>"
38        self.length = len(payload)
39        self._set_headers()
40        self.wfile.write(payload)
41
42def main():
43    httpd = HTTPServerV6(("", PORT), RequestHandler)
44    print "Serving at port", PORT
45    httpd.socket = ssl.wrap_socket(httpd.socket,
46                                   certfile='./https-server.pem',
47                                   server_side=True)
48    httpd.serve_forever()
49
50if __name__ == '__main__':
51    main()
52