File indexing completed on 2024-07-18 23:17:44
0001 import os
0002 from http.server import HTTPServer, BaseHTTPRequestHandler
0003 import mimetypes
0004 import argparse
0005
0006 class Serv(BaseHTTPRequestHandler):
0007 def do_GET(self):
0008
0009 if self.path == '/':
0010 self.path = '/index.html'
0011
0012 if self.path == '/modules_data.json':
0013 self.path = '/modules_data.json'
0014
0015 try:
0016 file_path = self.path[1:]
0017
0018
0019 with open(file_path, 'rb') as file:
0020 file_to_open = file.read()
0021
0022
0023 mime_type, _ = mimetypes.guess_type(file_path)
0024
0025
0026 self.send_response(200)
0027 if mime_type:
0028 self.send_header("Content-type", mime_type)
0029 self.end_headers()
0030
0031
0032 self.wfile.write(file_to_open)
0033
0034 except FileNotFoundError:
0035
0036 self.send_response(404)
0037 self.send_header("Content-type", "text/html")
0038 self.end_headers()
0039 self.wfile.write(bytes("File not found", 'utf-8'))
0040
0041 except Exception as e:
0042
0043 self.send_response(500)
0044 self.send_header("Content-type", "text/html")
0045 self.end_headers()
0046 self.wfile.write(bytes(f"Internal server error: {str(e)}", 'utf-8'))
0047
0048 def run(server_class=HTTPServer, handler_class=Serv, port=65432):
0049
0050 server_address = ('localhost', port)
0051 httpd = server_class(server_address, handler_class)
0052 print(f"Server started at http://localhost:{port}")
0053 httpd.serve_forever()
0054
0055 if __name__ == "__main__":
0056
0057 parser = argparse.ArgumentParser(description='Start a simple HTTP server.')
0058 parser.add_argument('--port', type=int, default=65432, help='Port to serve on (default: 65432)')
0059 args = parser.parse_args()
0060
0061
0062 run(port=args.port)
0063