-
Notifications
You must be signed in to change notification settings - Fork 13
/
frontend_server.py
40 lines (31 loc) · 1.08 KB
/
frontend_server.py
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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
from os import path
from http.server import BaseHTTPRequestHandler, HTTPServer
class HTTPHandler(BaseHTTPRequestHandler):
index = ''
root = ''
def do_GET( self ):
if self.path == '/':
req = self.root + '/' + self.index;
else:
req = self.root + self.path
if not path.isfile(req):
self.send_error(404, 'file not found')
return
self.send_response(200)
if req.endswith('.html'):
self.send_header('Content-type', 'text/html')
elif req.endswith('.css'):
self.send_header('Content-type', 'text/css')
else:
self.send_header('Content-type', 'text/plain')
self.end_headers()
with open(req, "rb") as f:
self.wfile.write(f.read())
def run( root, port, index ):
HTTPHandler.index = index
HTTPHandler.root = root
httpd = HTTPServer(('', port), HTTPHandler)
print("Started frontend server, connect you browser to http://localhost:" + str(port))
httpd.serve_forever()