-
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathstatic_server.py
More file actions
executable file
·42 lines (34 loc) · 1.33 KB
/
static_server.py
File metadata and controls
executable file
·42 lines (34 loc) · 1.33 KB
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
42
#!/usr/bin/env python3
from http.server import HTTPServer, BaseHTTPRequestHandler
import os
class StaticServer(BaseHTTPRequestHandler):
def do_GET(self):
root = os.path.join(os.path.dirname(os.path.dirname(os.path.abspath(__file__))), 'html')
#print(self.path)
if self.path == '/':
filename = root + '/index.html'
else:
filename = root + self.path
self.send_response(200)
if filename[-4:] == '.css':
self.send_header('Content-type', 'text/css')
elif filename[-5:] == '.json':
self.send_header('Content-type', 'application/javascript')
elif filename[-3:] == '.js':
self.send_header('Content-type', 'application/javascript')
elif filename[-4:] == '.ico':
self.send_header('Content-type', 'image/x-icon')
else:
self.send_header('Content-type', 'text/html')
self.end_headers()
with open(filename, 'rb') as fh:
html = fh.read()
#html = bytes(html, 'utf8')
self.wfile.write(html)
def run(server_class=HTTPServer, handler_class=StaticServer, port=8000):
server_address = ('', port)
httpd = server_class(server_address, handler_class)
print('Starting httpd on port {}'.format(port))
httpd.serve_forever()
run()
# vim: expandtab