LoginSignup
2
3

More than 5 years have passed since last update.

Redis 最短入門(3)Pythonでも5分でAPI

Last updated at Posted at 2016-01-05

Pythonだともう少しサッパリとしますが、
URLはサッパリしてません。お好きな方を。
※エラーなどは知りませんがお試しで。

sudo yum -y install python pip
pip install redis

mkdir api; cd api

./cgi-bin/get.py を下記のように編集
(python 2 系 cgi-bin以下にいれるのと、実行権限chmod +x がいります)

python -m CGIHTTPServer &

curl http://0.0.0.0:8000/cgi-bin/get.py?id="key"

{"items": "value"}

※こちらも下準備でredisサーバーをたちあげて値をいれてください
redis-cli > SET "key" "value"

get.py
#!/usr/bin/python
# coding: utf-8

import redis
import json
import cgi

form = cgi.FieldStorage()
print 'Content-Type: text/html\n\n'
if form.has_key("id"):
    id=form["id"].value
    r=redis.Redis()
    value=r.get(id)
    print json.dumps({'items':value})
2
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
2
3