forked from reactjs/react-tutorial
-
Notifications
You must be signed in to change notification settings - Fork 0
/
server.py
57 lines (48 loc) · 1.95 KB
/
server.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
# This file provided by Facebook is for non-commercial testing and evaluation purposes only.
# Facebook reserves all rights not expressly granted.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
# FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
import os
import json
import cgi
from BaseHTTPServer import HTTPServer
from SimpleHTTPServer import SimpleHTTPRequestHandler
PUBLIC_PATH = "public"
comments = json.loads(open('_comments.json').read())
def sendJSON(res):
res.send_response(200)
res.send_header('Content-type', 'application/json')
res.end_headers()
res.wfile.write(json.dumps(comments))
class MyHandler(SimpleHTTPRequestHandler):
def translate_path(self, path):
root = os.getcwd()
path = PUBLIC_PATH + path
return os.path.join(root, path)
def do_GET(self):
if (self.path == "/comments.json"):
sendJSON(self)
else:
SimpleHTTPRequestHandler.do_GET(self)
def do_POST(self):
if (self.path == "/comments.json"):
form = cgi.FieldStorage(
fp=self.rfile,
headers=self.headers,
environ={'REQUEST_METHOD':'POST',
'CONTENT_TYPE':self.headers['Content-Type']}
)
# Save the data
comments.append({u"author": form.getfirst("author"), u"text": form.getfirst("text")})
sendJSON(self)
else:
SimpleHTTPRequestHandler.do_POST(self)
if __name__ == '__main__':
print "Server started: http://localhost:3000/"
httpd = HTTPServer(('127.0.0.1', 3000), MyHandler)
httpd.serve_forever()