|
| 1 | +# This file provided by Facebook is for non-commercial testing and evaluation purposes only. |
| 2 | +# Facebook reserves all rights not expressly granted. |
| 3 | +# |
| 4 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 5 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 6 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL |
| 7 | +# FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN |
| 8 | +# ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION |
| 9 | +# WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
| 10 | + |
| 11 | +import os |
| 12 | +import json |
| 13 | +import cgi |
| 14 | +from BaseHTTPServer import HTTPServer |
| 15 | +from SimpleHTTPServer import SimpleHTTPRequestHandler |
| 16 | + |
| 17 | +PUBLIC_PATH = "public" |
| 18 | + |
| 19 | +comments = json.loads(open('_comments.json').read()) |
| 20 | + |
| 21 | +def sendJSON(res): |
| 22 | + res.send_response(200) |
| 23 | + res.send_header('Content-type', 'application/json') |
| 24 | + res.end_headers() |
| 25 | + res.wfile.write(json.dumps(comments)) |
| 26 | + |
| 27 | +class MyHandler(SimpleHTTPRequestHandler): |
| 28 | + def translate_path(self, path): |
| 29 | + root = os.getcwd() |
| 30 | + path = PUBLIC_PATH + path |
| 31 | + return os.path.join(root, path) |
| 32 | + |
| 33 | + def do_GET(self): |
| 34 | + if (self.path == "/comments.json"): |
| 35 | + sendJSON(self) |
| 36 | + else: |
| 37 | + SimpleHTTPRequestHandler.do_GET(self) |
| 38 | + |
| 39 | + def do_POST(self): |
| 40 | + if (self.path == "/comments.json"): |
| 41 | + form = cgi.FieldStorage( |
| 42 | + fp=self.rfile, |
| 43 | + headers=self.headers, |
| 44 | + environ={'REQUEST_METHOD':'POST', |
| 45 | + 'CONTENT_TYPE':self.headers['Content-Type']} |
| 46 | + ) |
| 47 | + |
| 48 | + # Save the data |
| 49 | + comments.append({u"author": form.getfirst("author"), u"text": form.getfirst("text")}) |
| 50 | + sendJSON(self) |
| 51 | + else: |
| 52 | + SimpleHTTPRequestHandler.do_POST(self) |
| 53 | + |
| 54 | +if __name__ == '__main__': |
| 55 | + print "Server started: http://localhost:3000/" |
| 56 | + httpd = HTTPServer(('127.0.0.1', 3000), MyHandler) |
| 57 | + httpd.serve_forever() |
0 commit comments