Redis の WebAPI (python CGI)

こちらで定めた仕様を満たすサーバーサイドのプログラムです。
Nginx + fcgiwrap で動作を確認しました。
[Redis の WebAPI を作成](https://qiita.com/ekzemplaro/items/1746b31beef382023f2d)

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

```bash
sudo apt install python3-redis
```

## プログラム

```py: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")
#
# --------------------------------------------------------
```

```py: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")
#
# --------------------------------------------------------
```

```py: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")
#
# --------------------------------------------------------
```

## テストスクリプト

```bash:go_list.sh
#
URL=http://localhost/test_dir/api/redis_list.py
#
curl -X POST $URL
#
http $URL
```

```bash: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"}'
```

```bash:go_read.sh
#
URL=http://localhost/test_dir/api/redis_read.py
#
curl -X POST -d key="t1855" $URL
#
echo
#
http $URL key==t1856
```

## 確認したバージョン

```text
$ python --version
Python 3.11.2
```