forked from plux/pluxtris
-
Notifications
You must be signed in to change notification settings - Fork 0
/
srv.py
53 lines (46 loc) · 1.9 KB
/
srv.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
from BaseHTTPServer import HTTPServer
from BaseHTTPServer import BaseHTTPRequestHandler
import json
class MyReqHandler(BaseHTTPRequestHandler):
fields = {}
def do_GET(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers( )
if (self.path == '/tetris.html' or
self.path == '/tetris' or
self.path == '/tetris.js' or
self.path == '/tetris.css' or
self.path == '/ajax.html' or
self.path == '/json2.js' or
self.path == '/ajax.js'):
f = open('.'+self.path)
content = f.read()
self.wfile.write(content)
elif (self.path == '/ajaxresponse'):
self.wfile.write("This is an ajax response")
else:
self.wfile.write("<html><body>")
self.wfile.write("<b>Hello</b>")
self.wfile.write(self.path)
self.wfile.write("</html></body>")
def do_POST(self):
self.send_response(200)
self.send_header("Content-type", "text/html")
self.end_headers( )
if (self.path == '/tetrisupdate'):
if self.headers.dict.has_key("content-length"):
content_length = int(self.headers.dict["content-length"])
raw_post_data = self.rfile.read(content_length)
data = json.loads(raw_post_data)
self.wfile.write(data["update"]["field"])
else:
self.wfile.write("<html><body>")
self.wfile.write("<b>Post Data:</b><br>")
if self.headers.dict.has_key("content-length"):
content_length = int(self.headers.dict["content-length"])
raw_post_data = self.rfile.read(content_length)
self.wfile.write(raw_post_data)
httpd = HTTPServer(('', 8000), MyReqHandler)
while True:
httpd.handle_request();