-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwebserver.py
executable file
·58 lines (56 loc) · 1.98 KB
/
webserver.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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Python 3 server example
import http.server as server
import os
import webbrowser
hostName = "localhost"
serverPort = 8000
class HTTPRequestHandler(server.SimpleHTTPRequestHandler):
def do_GET(self):
path = "docs/" + self.path[1:].split("?")[0]
if os.path.isfile(path):
self.send_response(200)
self.send_header("Content-type", self.guess_type(path))
self.end_headers()
f = open(path, 'rb')
if f:
try:
self.copyfile(f, self.wfile)
finally:
f.close()
elif os.path.isfile(path + ".html"):
self.send_response(200)
self.send_header("Content-type", self.guess_type(path + ".html"))
self.end_headers()
f = open(path + ".html", 'rb')
if f:
try:
self.copyfile(f, self.wfile)
finally:
f.close()
elif os.path.isdir(path):
self.send_response(200)
self.send_header("Content-type", self.guess_type(path + "/index.html"))
self.end_headers()
f = open(path + "/index.html", 'rb')
if f:
try:
self.copyfile(f, self.wfile)
finally:
f.close()
elif os.path.isfile("docs/404.html"):
self.send_response(404)
self.send_header("Content-type", self.guess_type("docs/404.html"))
self.end_headers()
f = open("docs/404.html", 'rb')
if f:
try:
self.copyfile(f, self.wfile)
finally:
f.close()
else:
self.send_response(404)
self.send_header("Content-type", "text/html")
self.end_headers()
self.wfile.write(bytes(path + " does not exist", "latin-1"))
if __name__ == '__main__':
server.test(HandlerClass=HTTPRequestHandler)