home · contact · privacy
Set up basic web server infrastructure.
authorChristian Heller <c.heller@plomlompom.de>
Sat, 16 Mar 2024 20:35:10 +0000 (21:35 +0100)
committerChristian Heller <c.heller@plomlompom.de>
Sat, 16 Mar 2024 20:35:10 +0000 (21:35 +0100)
task.py [new file with mode: 0755]

diff --git a/task.py b/task.py
new file mode 100755 (executable)
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 = '<html><body><p>hi there!</p></body></html>'
+        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)