pythonを用いたjsonを返却する簡易mockserver
WSGI (Web Server Gateway Interface) を用いてWeb サーバを起動する
from wsgiref.simple_server import make_server
import json
PORT = 8081
SETTING = [{"PATH":"/api/1","VALUE":
{"items":[
{"item1":"test1"},
{"item2":"test2"}
]}},
{"PATH":"/api/2","VALUE":
{"items2":[
{"itemA":"test1"},
{"itemB":"test2"}
]}}
]
def app(environ, start_response):
path = environ["PATH_INFO"]
for setting in SETTING:
if path.startswith(setting["PATH"]):
status = '200 OK'
headers = [
('Content-type', 'application/json; charset=utf-8'),
('Access-Control-Allow-Origin', '*'),
]
start_response(status, headers)
return [json.dumps(
setting["VALUE"]
).encode("utf-8")]
if __name__ == "__main__":
httpd = make_server('', PORT, app)
httpd.serve_forever()
参考
Tags: