diff options
author | Egor Tensin <Egor.Tensin@gmail.com> | 2022-04-11 01:26:12 +0200 |
---|---|---|
committer | Egor Tensin <Egor.Tensin@gmail.com> | 2022-04-11 01:26:12 +0200 |
commit | 609fea008b0091715bcdba1239b8126069c3473e (patch) | |
tree | 9c3825d03eec7032b052e4cf64818dc69bbcd54a /server.py | |
parent | index.html: use Bootstrap utilities (diff) | |
download | linux-status-609fea008b0091715bcdba1239b8126069c3473e.tar.gz linux-status-609fea008b0091715bcdba1239b8126069c3473e.zip |
put things into proper directories
Diffstat (limited to 'server.py')
-rwxr-xr-x | server.py | 75 |
1 files changed, 0 insertions, 75 deletions
diff --git a/server.py b/server.py deleted file mode 100755 index 914765d..0000000 --- a/server.py +++ /dev/null @@ -1,75 +0,0 @@ -#!/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 argparse -import http.server -import os -import sys -import traceback - -from app import Request, Response - - -DEFAULT_PORT = 18101 - - -def script_dir(): - return os.path.dirname(os.path.realpath(__file__)) - - -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() - response.write_to_request_handler(self) - except: - status = http.server.HTTPStatus.INTERNAL_SERVER_ERROR - response = Response(traceback.format_exc(), status) - response.write_to_request_handler(self) - return - - -def make_server(port): - addr = ('', port) - server = http.server.HTTPServer - if sys.version_info >= (3, 7): - server = http.server.ThreadingHTTPServer - return server(addr, RequestHandler) - - -def parse_args(args=None): - if args is None: - args = sys.argv[1:] - parser = argparse.ArgumentParser() - parser.add_argument('-p', '--port', metavar='PORT', - type=int, default=DEFAULT_PORT, - help='set port number') - return parser.parse_args(args) - - -def main(args=None): - # It's a failsafe; this script is only allowed to serve the directory it - # resides in. - os.chdir(script_dir()) - - args = parse_args(args) - httpd = make_server(args.port) - try: - httpd.serve_forever() - except KeyboardInterrupt: - print('\nKeyboard interrupt received, exiting...') - - -if __name__ == '__main__': - main() |