CMS 3D CMS Logo

server.py
Go to the documentation of this file.
1 from http.server import HTTPServer, BaseHTTPRequestHandler
2 import os
3 import mimetypes
4 
5 class Serv(BaseHTTPRequestHandler):
6  def do_GET(self):
7  if self.path == '/':
8  self.path = '/index.html'
9 
10  try:
11  file_path = self.path[1:]
12 
13  with open(file_path, 'rb') as file:
14  file_to_open = file.read()
15 
16  mime_type, _ = mimetypes.guess_type(file_path)
17 
18  self.send_response(200)
19  if mime_type:
20  self.send_header("Content-type", mime_type)
21  self.end_headers()
22 
23  self.wfile.write(file_to_open)
24 
25  except FileNotFoundError:
26  self.send_response(404)
27  self.send_header("Content-type", "text/html")
28  self.end_headers()
29  self.wfile.write(bytes("File not found", 'utf-8'))
30 
31  except Exception as e:
32  self.send_response(500)
33  self.send_header("Content-type", "text/html")
34  self.end_headers()
35  self.wfile.write(bytes(f"Internal server error: {str(e)}", 'utf-8'))
36 
37 httpd = HTTPServer(('localhost', 65432), Serv)
38 print("Server started at http://localhost:65432")
39 httpd.serve_forever()
void print(TMatrixD &m, const char *label=nullptr, bool mathematicaFormat=false)
Definition: Utilities.cc:47
def do_GET(self)
Definition: server.py:6