Gary Wu | 9abb61c | 2018-09-27 10:38:50 -0700 | [diff] [blame^] | 1 | import ssl |
| 2 | from http.server import BaseHTTPRequestHandler, HTTPServer |
| 3 | |
| 4 | from sys import argv |
| 5 | |
| 6 | DEFAULT_PORT = 8443 |
| 7 | |
| 8 | |
| 9 | class SDCHandler(BaseHTTPRequestHandler): |
| 10 | |
| 11 | def __init__(self, request, client_address, server): |
| 12 | self.response_on_get = self._read_on_get_response() |
| 13 | super().__init__(request, client_address, server) |
| 14 | |
| 15 | def do_GET(self): |
| 16 | self.send_response(200) |
| 17 | self._set_headers() |
| 18 | |
| 19 | self.wfile.write(self.response_on_get.encode("utf-8")) |
| 20 | return |
| 21 | |
| 22 | def _set_headers(self): |
| 23 | self.send_header('Content-Type', 'application/json') |
| 24 | self.end_headers() |
| 25 | |
| 26 | @staticmethod |
| 27 | def _read_on_get_response(): |
| 28 | with open('sdc_get_response.json', 'r') as file: |
| 29 | return file.read() |
| 30 | |
| 31 | |
| 32 | if __name__ == '__main__': |
| 33 | SDCHandler.protocol_version = "HTTP/1.1" |
| 34 | |
| 35 | httpd = HTTPServer(('', DEFAULT_PORT), SDCHandler) |
| 36 | httpd.socket = ssl.wrap_socket(httpd.socket, server_side=True, certfile='cert.pem', keyfile='key.pem') |
| 37 | httpd.serve_forever() |