Skip to content

Instantly share code, notes, and snippets.

@ProfAvery
Created April 25, 2013 06:30
Show Gist options
  • Save ProfAvery/5457907 to your computer and use it in GitHub Desktop.
Save ProfAvery/5457907 to your computer and use it in GitHub Desktop.
Magic 8-Ball example (Bottle, Redis, JSON)
#!/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