サーバーサイドプログラム
Nginx + fcgiwrap という環境で作動します。
Get と Post の両方に対応します。
aa と bb を与えて、和と差を計算して返します。
test_aa.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# text_aa.py
#
# Nov/20/2019
#
# ---------------------------------------------------------------------
import sys
import json
import cgi
import cgitb
cgitb.enable()
#
# ---------------------------------------------------------------------
ff=cgi.FieldStorage()
#
str_aa = ff.getfirst("aa","")
str_bb = ff.getfirst("bb","")
aa = int(str_aa)
bb = int(str_bb)
wa = aa + bb
sa = aa - bb
#
dict_aa = {}
dict_aa['aa'] = aa
dict_aa['bb'] = bb
dict_aa['wa'] = wa
dict_aa['sa'] = sa
str_aa = json.dumps (dict_aa)
#
print ("Content-type: text/json; charset=UTF-8\n\n")
print (str_aa)
# ---------------------------------------------------------------------
curl の Get でテスト
curl "http://example.com/test_aa.py?aa=81&bb=34"
Python の Get のクライアント
http_get.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# http_get.py
#
# Nov/11/2019
#
# ------------------------------------------------------------------
import sys
import json
import requests
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
url="http://example.com/test_aa.py"
args={}
args['aa']= 891
args['bb']= 192
#
rr=requests.get(url,args)
dict_data = json.loads(rr.text)
#
print(dict_data['aa'])
print(dict_data['bb'])
print(dict_data['wa'])
print(dict_data['sa'])
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------
Python の Post のクライアント
http_post.py
#! /usr/bin/python
# -*- coding: utf-8 -*-
#
# http_post.py
#
# Nov/11/2019
#
# ------------------------------------------------------------------
import sys
import json
import requests
# ------------------------------------------------------------------
sys.stderr.write("*** 開始 ***\n")
#
url="http://example.com/test_aa.py"
args={}
args['aa']= 875
args['bb']= 512
#
rr=requests.post(url,args)
dict_data = json.loads(rr.text)
#
print(dict_data['aa'])
print(dict_data['bb'])
print(dict_data['wa'])
print(dict_data['sa'])
#
sys.stderr.write("*** 終了 ***\n")
# ------------------------------------------------------------------