From 5744cbdb67c253d8a8d54eb9710958a66a64fbb6 Mon Sep 17 00:00:00 2001 From: Christian Heller Date: Sat, 16 Mar 2024 21:35:10 +0100 Subject: [PATCH 1/1] Set up basic web server infrastructure. --- task.py | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100755 task.py 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) -- 2.30.2