Redis 的 WebAPI (Python CGI)
这是一个符合我们在这里设定的规格的服务器端程序。
已经在Nginx和fcgiwrap上进行了测试。
创建了Redis的WebAPI。
安装图书馆
sudo apt install python3-redis
编程
#! /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")
#
# --------------------------------------------------------
#! /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")
#
# --------------------------------------------------------
#! /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")
#
# --------------------------------------------------------
测试脚本
#
URL=http://localhost/test_dir/api/redis_list.py
#
curl -X POST $URL
#
http $URL
#
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"}'
#
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