Created
April 25, 2013 06:30
-
-
Save ProfAvery/5457907 to your computer and use it in GitHub Desktop.
Magic 8-Ball example (Bottle, Redis, JSON)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
import random | |
from bottle import route, run, request, install | |
from bottle_redis import RedisPlugin | |
install(RedisPlugin()) | |
@route('/ask') | |
def read_json(rdb): | |
nchoices = rdb.llen('answers') | |
answers = rdb.lrange('answers', 0, nchoices - 1) | |
return { 'answer': random.choice(answers) } | |
@route('/answers', method='POST') | |
def write_json(rdb): | |
rdb.delete('answers') | |
for answer in request.json['answers']: | |
rdb.lpush('answers', answer) | |
run(host='0.0.0.0', port=8080, debug=True, reloader=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment