Skip to content

Commit

Permalink
Switch python server to use Flask
Browse files Browse the repository at this point in the history
  • Loading branch information
dbunker committed Feb 3, 2015
1 parent 558e0c7 commit 8e78f6c
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 45 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ node server.js
### Python

```sh
pip install -r requirements.txt
python server.py
```

Expand Down
1 change: 1 addition & 0 deletions requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Flask==0.10.1
58 changes: 13 additions & 45 deletions server.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,57 +8,25 @@
# 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
from flask import Flask, Response, request

PUBLIC_PATH = "public"
app = Flask(__name__, static_url_path='', static_folder='public')
app.add_url_rule('/', 'root', lambda: app.send_static_file('index.html'))

file = open('_comments.json', 'r+')
comments = json.loads(file.read())
file.close()
@app.route('/comments.json', methods=['GET', 'POST'])
def comments_handler():

def sendJSON(res):
res.send_response(200)
res.send_header('Content-type', 'application/json')
res.end_headers()
res.wfile.write(json.dumps(comments))
with open('_comments.json', 'r') as file:
comments = json.loads(file.read())

class MyHandler(SimpleHTTPRequestHandler):
def translate_path(self, path):
root = os.getcwd()
path = PUBLIC_PATH + path
return os.path.join(root, path)
if request.method == 'POST':
comments.append(request.form.to_dict())

def do_GET(self):
if (self.path == "/comments.json"):
sendJSON(self)
else:
SimpleHTTPRequestHandler.do_GET(self)
with open('_comments.json', 'w') as file:
file.write(json.dumps(comments, indent=4, separators=(',', ': ')))

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")})
# Write to file
file = open('_comments.json', 'w+')
file.write(json.dumps(comments))
file.close()

sendJSON(self)
else:
SimpleHTTPRequestHandler.do_POST(self)
return Response(json.dumps(comments), mimetype='application/json')

if __name__ == '__main__':
print "Server started: http://localhost:3000/"
httpd = HTTPServer(('127.0.0.1', 3000), MyHandler)
httpd.serve_forever()
app.run(port=3000)

0 comments on commit 8e78f6c

Please sign in to comment.