Depuis le proxy Redis de Flask

#!/usr/bin/env python
import os
import redis
from flask import Flask, request
import json
app = Flask(__name__)
app.debug = True

@app.route('/')
def redis_input():
  html = '''
  <!doctype html>
  <form action="/api/post" method="post">
  Key:<input type=text name="key"><br>
  Val:<input type=text name="val"><br>
  <input type=submit value="save">
  </form>
  '''
  return html

# GET method ================================================
@app.route('/api/set/<key>/<val>')
def set_id(key, val):
  gram = 'redis_' + key
  twit_id = str(val)
  r = redis.Redis(host='localhost', port=6379, db=0)
  r.rpush(gram, twit_id)
  cur_list = r.lrange(gram, 0, -1)
  return json.dumps(cur_list, indent=2)

@app.route('/api/get/<key>')
def get_id(key):
  gram = 'redis_' + key
  r = redis.Redis(host='localhost', port=6379, db=0)
  cur_list = r.lrange(gram, 0, -1)
  return json.dumps(cur_list, indent=2) 

# POST method ================================================
@app.route('/api/post', methods=['POST'])
def set_post_id():
  gram = 'redis_' + request.form['key']
  twit_id = str(request.form['val'])
  r = redis.Redis(host='localhost', port=6379, db=0)
  r.rpush(gram, twit_id)
  cur_list = r.lrange(gram, 0, -1)
  return json.dumps(cur_list, indent=2)

if __name__ == '__main__':
  app.run(host='0.0.0.0', port=8080)

Recommended Posts

Depuis le proxy Redis de Flask
ballon
Premier flacon
Mémo du flacon