This is a server-side program that meets the specifications specified here. Create Redis WebAPI
bottle_redis.py
#! /usr/bin/python
#
# bottle_redis.py
#
# Nov/13/2020
#
# --------------------------------------------------------
from bottle import route, run, post, request
import redis
import json
import sys
# --------------------------------------------------------
@post('/read')
def read():
key = request.forms.get('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")
return json_str
#
# --------------------------------------------------------
@post('/insert')
def insert():
key = request.forms.key
json_str = request.forms.value
#
sys.stderr.write(key + "\n")
sys.stderr.write(json_str + "\n")
#
if json_str != "0000":
rr = redis.Redis(host='localhost', port=6379, db=0)
rr.set(key, json_str)
#
str_out = ""
str_out += key + " *** "
str_out += json_str + " *** "
return str_out
#
# --------------------------------------------------------
@post('/list')
def list():
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")
#
return json_str
#
# --------------------------------------------------------
run(host='localhost', port=8080, debug=True)
# --------------------------------------------------------
Start the server
$ ./bottle_redis.py
Bottle v0.12.18 server starting up (using WSGIRefServer())...
Listening on http://localhost:8080/
Hit Ctrl-C to quit.
reading
curl -X POST -d key=t1855 http://localhost:8080/read
writing
curl -X POST \
-d key=t1855 \
-d value='{"name": "Utsunomiya","population": 87516,"date_mod": "2001-3-16"}' \
http://localhost:8080/insert
List of keys
curl -X POST http://localhost:8080/list
Recommended Posts