home · contact · privacy
Add small script for setting up a proper development environment.
[plomtask] / task.py
1 #!/usr/bin/env python3
2 """
3 plom's task manager
4 """
5 from http.server import BaseHTTPRequestHandler
6 from http.server import HTTPServer
7
8 HTTP_PORT = 8082
9
10
11 class HandledException(Exception):
12     """To identify Exceptions based on expected (if faulty) user behavior."""
13
14
15 class TaskHandler(BaseHTTPRequestHandler):
16     """Handles single HTTP request."""
17
18     def do_GET(self):
19         """Handle any GET request."""
20         self.send_response(200)
21         self.end_headers()
22         html = '<html><body><p>hi there!</p></body></html>'
23         self.wfile.write(bytes(html, 'utf-8'))
24
25
26 def main():
27     """Main loop."""
28     server = HTTPServer(('localhost', HTTP_PORT), TaskHandler)
29     print(f'running at port {HTTP_PORT}')
30     try:
31         server.serve_forever()
32     except KeyboardInterrupt:
33         print('aborting due to keyboard interrupt')
34     server.server_close()
35
36
37 if __name__ == '__main__':
38     from sys import exit as sys_exit
39     try:
40         main()
41     except HandledException as e:
42         print(f'Aborting due to: {e}')
43         sys_exit(1)