開発の人に見せたらすごく笑われてしまいますが、ちょっと勉強用のため、ここにメモを残しておきます。
サンプルは、python3でMySQLからSELECTした結果をjsonで返すものです。自分的にapi (笑)的なもの
- port 5000番で待ち受けます
- port 8080から上記をCALLします
ってな感じで汗
仮に、port:5000 を apiサーバとしてw port: 8080 をCALL側として書いた例です
うーん、、プログラムってむずい。。。

ソースコード
apiサーバ
from flask import Flask, render_template, request, jsonify
import MySQLdb
app = Flask(__name__)
_host = "db-v403.testtest.local"
_uid = "root"
_pwd = "passsswwwwwwwdddddd"
_schema = "mydb"
def getConnection():
return MySQLdb.connect(
host=_host,
user=_uid,
password=_pwd,
db=_schema,
charset='utf8',
)
@app.route('/')
def index():
return render_template('index.html')
@app.route('/dblists') #Methodを明示する必要あり
def dblists():
cn = getConnection()
c = cn.cursor(MySQLdb.cursors.DictCursor)
_sql = '''
SELECT
s.Id As id,
v.Title AS title,
v.Artist AS artist
FROM
v4.dblists v
INNER JOIN
work.lists s
ON
v.Id = s.dblistId
ORDER BY
s.Id
LIMIT 100
;
'''
c.execute(_sql)
dblists = c.fetchall()
c.close()
cn.close()
return jsonify({
'status':'OK',
'dblists':dblists
})
if __name__ == "__main__":
# app.run(debug=True)
app.run(host='0.0.0.0', port=5000)
call側
from flask import Flask, render_template, request, jsonify
import json
import urllib.request
app = Flask(__name__)
@app.route('/')
def index():
try:
url = 'http://localhost:5000/dblists'
res = urllib.request.urlopen(url)
data = json.loads(res.read().decode('utf-8'))
except urllib.error.HTTPError as e:
print('HTTPError: ', e)
except json.JSONDecodeError as e:
print('JSONDecodeError: ', e)
return render_template('index.html', data=data)
if __name__ == "__main__":
# app.run(debug=True)
app.run(host='0.0.0.0', port=8080)
こんな感じいいのだろうか?汗。。。