1
3

More than 3 years have passed since last update.

Python の http サーバーサイド処理

Last updated at Posted at 2019-11-11

サーバーサイドプログラム

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")
# ------------------------------------------------------------------

参考ページ
cgi --- CGI (ゲートウェイインタフェース規格) のサポート

1
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
3