-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathserver.py
39 lines (28 loc) · 962 Bytes
/
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
import os
import urllib
from http.server import HTTPServer, SimpleHTTPRequestHandler
dir_path = os.path.dirname(os.path.realpath(__file__))
addr = "0.0.0.0"
port = 9000
os.chdir(os.path.join(dir_path, "public"))
file = open("index.html", "r")
content = file.read()
# HTTPRequestHandler class
class RequestHandler(SimpleHTTPRequestHandler):
# GET
def do_GET(self):
parsedParams = urllib.parse.urlparse(self.path)
if os.access('.' + os.sep + parsedParams.path, os.R_OK):
SimpleHTTPRequestHandler.do_GET(self);
return
# Send response status code
self.send_response(200)
self.send_header('Content-Type','text/html')
self.end_headers()
self.wfile.write(content.encode())
return
def run(server_class=HTTPServer, handler=RequestHandler):
print("running on {}:{}".format(addr, port))
httpd = server_class((addr, port), handler)
httpd.serve_forever()
run()