File tree Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Expand file tree Collapse file tree 1 file changed +47
-0
lines changed Original file line number Diff line number Diff line change
1
+ import os
2
+ import json
3
+ import cgi
4
+ from BaseHTTPServer import HTTPServer
5
+ from SimpleHTTPServer import SimpleHTTPRequestHandler
6
+
7
+ PUBLIC_PATH = "public"
8
+
9
+ comments = json .loads (open ('_comments.json' ).read ())
10
+
11
+ def sendJSON (res ):
12
+ res .send_response (200 )
13
+ res .send_header ('Content-type' , 'application/json' )
14
+ res .end_headers ()
15
+ res .wfile .write (json .dumps (comments ))
16
+
17
+ class MyHandler (SimpleHTTPRequestHandler ):
18
+ def translate_path (self , path ):
19
+ root = os .getcwd ()
20
+ path = PUBLIC_PATH + path
21
+ return os .path .join (root , path )
22
+
23
+ def do_GET (self ):
24
+ if (self .path == "/comments.json" ):
25
+ sendJSON (self )
26
+ else :
27
+ SimpleHTTPRequestHandler .do_GET (self )
28
+
29
+ def do_POST (self ):
30
+ if (self .path == "/comments.json" ):
31
+ form = cgi .FieldStorage (
32
+ fp = self .rfile ,
33
+ headers = self .headers ,
34
+ environ = {'REQUEST_METHOD' :'POST' ,
35
+ 'CONTENT_TYPE' :self .headers ['Content-Type' ]}
36
+ )
37
+
38
+ # Save the data
39
+ comments .append ({u"author" : form .getfirst ("author" ), u"text" : form .getfirst ("text" )})
40
+ sendJSON (self )
41
+ else :
42
+ SimpleHTTPRequestHandler .do_POST (self )
43
+
44
+ if __name__ == '__main__' :
45
+ print "Server started: http://localhost:3000/"
46
+ httpd = HTTPServer (('127.0.0.1' , 3000 ), MyHandler )
47
+ httpd .serve_forever ()
You can’t perform that action at this time.
0 commit comments