From: Christian Heller Date: Sat, 16 Mar 2024 20:35:10 +0000 (+0100) Subject: Set up basic web server infrastructure. X-Git-Url: https://plomlompom.com/repos/%7B%7Bdb.prefix%7D%7D/%7B%7B%20web_path%20%7D%7D/todos?a=commitdiff_plain;h=5744cbdb67c253d8a8d54eb9710958a66a64fbb6;p=plomtask Set up basic web server infrastructure. --- 5744cbdb67c253d8a8d54eb9710958a66a64fbb6 diff --git a/task.py b/task.py new file mode 100755 index 0000000..a1510b0 --- /dev/null +++ b/task.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 +""" +plom's task manager +""" +from http.server import BaseHTTPRequestHandler +from http.server import HTTPServer + +HTTP_PORT = 8082 + + +class HandledException(Exception): + """To identify Exceptions based on expected (if faulty) user behavior.""" + + +class TaskHandler(BaseHTTPRequestHandler): + """Handles single HTTP request.""" + + def do_GET(self): + """Handle any GET request.""" + self.send_response(200) + self.end_headers() + html = '

hi there!

' + self.wfile.write(bytes(html, 'utf-8')) + + +def main(): + """Main loop.""" + server = HTTPServer(('localhost', HTTP_PORT), TaskHandler) + print(f'running at port {HTTP_PORT}') + try: + server.serve_forever() + except KeyboardInterrupt: + print('aborting due to keyboard interrupt') + server.server_close() + + +if __name__ == '__main__': + from sys import exit as sys_exit + try: + main() + except HandledException as e: + print(f'Aborting due to: {e}') + sys_exit(1)