Redis の WebAPI (python CGI)


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

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")
#
# --------------------------------------------------------