0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Redis の WebAPI (python CGI)

Last updated at Posted at 2020-01-14

こちらで定めた仕様を満たすサーバーサイドのプログラムです。
Nginx + fcgiwrap で動作を確認しました。
Redis の WebAPI を作成

ライブラリーのインストール

sudo apt install python3-redis

プログラム

redis_read.py
#! /usr/bin/python3
# -*- coding: utf-8 -*-
#
#	test_dir/api/redis_read.py
#
#					Jan/14/2020
#
# --------------------------------------------------------
import	sys
import	os
import	string
import  json
import  redis
import  cgi
import	cgitb
cgitb.enable()
#
# --------------------------------------------------------
def get_key_proc():
	ff=cgi.FieldStorage()
	if "key" in ff:
		value = ff.getfirst("key","")
	else:
		value = "t1851"
#
	return value
# --------------------------------------------------------
sys.stderr.write("*** redis_read.py *** start ***\n")
#
key = "0000"
#
try:
	key = get_key_proc()
except Exception as ee:
	sys.stderr.write("*** error *** in get_key_proc ***\n")
	sys.stderr.write(str(ee) + "\n")
#
sys.stderr.write("*** redis_read.py *** key = %s\n" % key)
#
json_str = '{}'
#
try:
	rr = redis.Redis(host='localhost', port=6379, db=0)
	json_str = rr.get(key).decode()
except Exception as ee:
	sys.stderr.write("*** error *** in rr.get ***\n")
	sys.stderr.write(str(ee) + "\n")

print ("Content-type: text/json; charset=UTF-8\n\n")
print (json_str)

sys.stderr.write("*** redis_read.py *** end ***\n")
#
# --------------------------------------------------------
redis_insert.py
#! /usr/bin/python3
# -*- coding: utf-8 -*-
#
#	test_dir/api/redis_insert.py
#
#					Jan/14/2020
#
# --------------------------------------------------------
import	sys
import	os
import	string
import  json
import  redis
import  cgi
import	cgitb
cgitb.enable()
#
#
# --------------------------------------------------------
def get_string_proc(ff,key):
    if key in ff:
        value = ff.getfirst(key,"")
    else:
        value = "0000"
#
    return value
# --------------------------------------------------------
sys.stderr.write("*** redis_insert.py *** start *** \n")
#
ff=cgi.FieldStorage()
#
key = get_string_proc(ff,"key")
json_str = get_string_proc(ff,"value")
#
if json_str != "0000":
	rr = redis.Redis(host='localhost', port=6379, db=0)
	rr.set(key, json_str)
#
print ("Content-type: text/json; charset=UTF-8\n\n")
print (json_str)

sys.stderr.write("*** redis_insert.py *** end *** \n")
#
# --------------------------------------------------------
redis_list.py
#! /usr/bin/python3
# -*- coding: utf-8 -*-
#
#	test_dir/api/redis_list.py
#
#					Jan/14/2020
#
# --------------------------------------------------------
import	sys
import	os
import	string
import  json
import  redis
import  cgi
import	cgitb
cgitb.enable()
#
# --------------------------------------------------------
sys.stderr.write("*** redis_list.py *** start ***\n")
#
keys = []
json_str = ""
#
try:
	rr = redis.Redis(host='localhost', port=6379, db=0)
	keys = rr.keys('*')
except Exception as ee:
	sys.stderr.write("*** error *** in rr.keys ***\n")
	sys.stderr.write(str(ee) + "\n")
#
keys_str = []
for key in keys:
	key_str = key.decode('utf-8')
	keys_str.append(key_str)
#
try:
	json_str = json.dumps(keys_str)
except Exception as ee:
	sys.stderr.write("*** error *** in json.dumps ***\n")
	sys.stderr.write(str(ee) + "\n")

print ("Content-type: text/json; charset=UTF-8\n\n")
print (json_str)

sys.stderr.write("*** redis_list.py *** end ***\n")
#
# --------------------------------------------------------

テストスクリプト

go_list.sh
#
URL=http://localhost/test_dir/api/redis_list.py
#
curl -X POST $URL
#
http $URL
go_insert.sh
#
URL=http://localhost/test_dir/api/redis_insert.py
#
curl -X POST -d key="t1855" \
	-d value="{"name": "宇都宮", "population": 41295, "date_mod": "2003-8-12"}" \
	$URL
#
echo
#
http $URL key==t1856 \
	value=='{"name": "小山", "population": 39671, "date_mod": "2003-5-24"}'
go_read.sh
#
URL=http://localhost/test_dir/api/redis_read.py
#
curl -X POST -d key="t1855" $URL
#
echo
#
http $URL key==t1856

確認したバージョン

$ python --version
Python 3.11.2
0
1
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
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?