forked from purescript-contrib/purescript-react
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
40 lines (31 loc) · 1.34 KB
/
server.py
File metadata and controls
40 lines (31 loc) · 1.34 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
# This is originally from https://github.com/nebstrebor/react-tutorial-simpleserver
# It was modified a bit in the process.
import BaseHTTPServer
import SimpleHTTPServer
import json
import urlparse
class ReactTutorialHTTPRequestHandler(SimpleHTTPServer.SimpleHTTPRequestHandler):
"""
Extension of SimpleHTTPRequestHandler that works with the react.js tutorial.
In addition to standard SimpleHTTPServer file-webserver functionality, adds
POST-ability.
USAGE: python server.py to serve files from the cwd
(works the same as running python -m SimpleHTTPServer in the directory)
"""
def do_POST(self):
# (1) get posted data & convert it to python dict
content_length = int(self.headers['Content-Length'])
post_data = dict(urlparse.parse_qsl(self.rfile.read(content_length).decode('utf-8')))
# (2) open the file at the requested URL (404 if bad)
with open(self.translate_path(self.path), 'r+w') as f:
comments = json.loads(f.read())
comments.append(post_data)
f.seek(0, 0)
f.write(json.dumps(comments))
f.truncate()
return self.do_GET()
def test(HandlerClass=ReactTutorialHTTPRequestHandler,
ServerClass=BaseHTTPServer.HTTPServer):
BaseHTTPServer.test(HandlerClass, ServerClass)
if __name__ == '__main__':
test()