aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/server.py
blob: 35099cfd017a80ff31fef07fdf05c328aa029f37 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3

# Copyright (c) 2021 Egor Tensin <Egor.Tensin@gmail.com>
# This file is part of the "linux-status" project.
# For details, see https://github.com/egor-tensin/linux-status.
# Distributed under the MIT License.

# This script launches a HTTP server and uses app.py for processing a set of
# custom URLs.  See that file for the reasons behind this.

import http.server

from app import Request


class RequestHandler(http.server.SimpleHTTPRequestHandler):
    def do_GET(self):
        try:
            request = Request.from_http_path(self.path)
        except ValueError:
            return super().do_GET()
        try:
            response = request.process()
        except:
            self.send_response(http.server.HTTPStatus.INTERNAL_SERVER_ERROR)
            self.end_headers()
            return
        response.write_as_request_handler(self)


def main():
    addr = ('', 18101)
    httpd = http.server.ThreadingHTTPServer(addr, RequestHandler)
    try:
        httpd.serve_forever()
    except KeyboardInterrupt:
        print('\nKeyboard interrupt received, exiting...')


if __name__ == '__main__':
    main()