-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
48 lines (42 loc) · 1.5 KB
/
server.py
File metadata and controls
48 lines (42 loc) · 1.5 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
from http.server import BaseHTTPRequestHandler,HTTPServer
PORT_NUMBER = 5000
class myHandler(BaseHTTPRequestHandler):
def do_GET(self):
if self.path == '/' or self.path == "":
self.path = "/index.html"
try:
sendReply = False
if self.path.endswith(".html"):
mimetype = "text/html"
sendReply = True
if self.path.endswith(".json"):
mimetype = "text/plain"
sendReply = True
if self.path.endswith(".js"):
mimetype = "application/javascript"
sendReply = True
if sendReply:
f = open("." + self.path)
self.send_response(200)
self.send_header("Content-type", mimetype)
self.end_headers()
self.wfile.write(bytes(f.read(), encoding="utf8"))
f.close()
return
except IOError:
self.send_error(404,"File Not Found: " + self.path)
def do_POST(self):
content_length = int(self.headers["Content-Length"])
post_data = self.rfile.read(content_length).decode("utf8")
print(post_data)
f = open('.' + self.path, 'w')
f.write(post_data)
f.close()
self.send_response(200)
self.send_header("Content-type", "")
self.end_headers()
try:
server = HTTPServer(('', PORT_NUMBER), myHandler)
server.serve_forever()
except KeyboardInterrupt:
server.socket.close()