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 42def main(address): 43 if ':' in address: 44 httpd = HTTPServerV6((address, PORT), RequestHandler) 45 else: 46 httpd = HTTPServer((address, PORT), RequestHandler) 47 48 print("Serving at port", PORT) 49 httpd.socket = ssl.wrap_socket (httpd.socket, 50 certfile='/net-tools/https-server.pem', 51 server_side=True) 52 httpd.serve_forever() 53 54if __name__ == '__main__': 55 if len(sys.argv) > 1: 56 main(sys.argv[1]) 57 else: 58 main('::') 59