3 from http.server import HTTPServer
6 def header_200_and_json(self):
7 self.send_response(200)
8 self.send_header('Content-Type', 'application/json')
11 def header_404_and_json(self):
12 self.send_response(404)
13 self.send_header('Content-Type', 'application/json')
16 def start_http_endpoint(port, handler_class):
17 _thread.start_new_thread(init_http_endpoints, (port, handler_class))
20 def start_https_endpoint(port, handler_class, keyfile, certfile, ca_certs):
21 _thread.start_new_thread(init_https_endpoints, (port, handler_class, keyfile, certfile, ca_certs))
24 def init_http_endpoints(port, handler_class, server_class=HTTPServer):
25 server = server_class(('', port), handler_class)
26 sa = server.socket.getsockname()
27 print("Serving HTTP on", sa[0], "port", sa[1], "for", handler_class, "...")
28 server.serve_forever()
31 def init_https_endpoints(port, handler_class, keyfile, certfile, ca_certs, server_class=HTTPServer):
32 server = server_class(('', port), handler_class)
33 server.socket = ssl.wrap_socket(server.socket, keyfile=keyfile, certfile=certfile,
34 ca_certs=ca_certs, server_side=True)
35 sa = server.socket.getsockname()
36 print("Serving HTTPS on", sa[0], "port", sa[1], "for", handler_class, "...")
37 server.serve_forever()