-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathserver.py
More file actions
56 lines (42 loc) · 1.51 KB
/
server.py
File metadata and controls
56 lines (42 loc) · 1.51 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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import os
from http.server import BaseHTTPRequestHandler
from routes.main import routes
from response.staticHandler import StaticHandler
from response.templateHandler import TemplateHandler
from response.badRequestHandler import BadRequestHandler
class Server(BaseHTTPRequestHandler):
def do_HEAD(self):
return
def do_GET(self):
split_path = os.path.splitext(self.path)
request_extension = split_path[1]
if request_extension is "" or request_extension is ".html":
if self.path in routes:
handler = TemplateHandler()
handler.find(routes[self.path])
else:
handler = BadRequestHandler()
elif request_extension is ".py":
handler = BadRequestHandler()
else:
handler = StaticHandler()
handler.find(self.path)
self.respond({
'handler': handler
})
def handle_http(self, handler):
status_code = handler.getStatus()
self.send_response(status_code)
if status_code is 200:
content = handler.getContents()
self.send_header('Content-type', handler.getContentType())
else:
content = "404 Not Found"
self.end_headers()
if isinstance(content, bytes):
return content
else:
return bytes(content, 'UTF-8')
def respond(self, opts):
response = self.handle_http(opts['handler'])
self.wfile.write(response)