1#!/usr/bin/env python3 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 http.server import HTTPServer, BaseHTTPRequestHandler 19import ssl 20import sys 21 22PORT = 4443 23 24class HTTPServerV6(HTTPServer): 25 address_family = socket.AF_INET6 26 27class RequestHandler(BaseHTTPRequestHandler): 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 42 def do_GET(self): 43 payload = "<html><p>Done</p></html>" 44 self.length = len(payload) 45 self._set_headers() 46 self.wfile.write(payload.encode()) 47 48def main(address): 49 if ':' in address: 50 httpd = HTTPServerV6((address, PORT), RequestHandler) 51 else: 52 httpd = HTTPServer((address, PORT), RequestHandler) 53 54 print("Serving at port", PORT) 55 httpd.socket = ssl.wrap_socket (httpd.socket, 56 certfile='/net-tools/https-server.pem', 57 server_side=True) 58 httpd.serve_forever() 59 60if __name__ == '__main__': 61 if len(sys.argv) > 1: 62 main(sys.argv[1]) 63 else: 64 main('::') 65