7. 체인코드 연동 Flask 웹어플리케이션 만들기 (2)
페이지 정보
작성자 관리자 댓글 0건 조회 2,954회 작성일 19-11-30 04:37본문
7. 체인코드 연동 Flask 웹어플리케이션 만들기 (2)
이번에는 REST API 서버를 만들고 Flask 웹어플리케이션으로 접속하는 실습을 하겠다.
REST API 어플리케이션 만들기
root@client:~/pyApp# mkdir myAPI
root@client:~/pyApp# cd myAPI/
root@client:~/pyApp/myAPI# vi api.py
from flask import Flask, jsonify
from flask_restful import Api, Resource
import json
import os
import sys
import subprocess
app = Flask(__name__)
api = Api(app)
class UserAPI(Resource):
def get(self, userid):
print(userid)
result = subprocess.check_output ('FABRIC_CFG_PATH=/root/hyperledger/fabric-samples/first-network/ docker exec cli scripts/query-users.sh '+userid , shell=True)
print(result)
userinfo = json.loads(result)
return jsonify(userinfo)
def put(self, userid):
pass
def delete(self, userid):
pass
api.add_resource(UserAPI, '/users/<string:userid>', endpoint = 'user')
if __name__ == '__main__':
app.debug = True
app.run(host='0.0.0.0', port=3000)
root@client:~/pyApp/myAPI# python api.py
* Serving Flask app "api" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://127.0.0.1:3000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 301-791-187
root@client:~/pyApp/myAPI# curl localhost:3000/users/admin
{
"AP": "0",
"CC": "0",
"NM": "LeeJinKwan",
"PN": "0",
"PW": "1234"
}
Flask 웹어플리케이션 만들기
root@client:~/pyApp/myAPI# cd ..
root@client:~/pyApp# mkdir -p myPS/templates
root@client:~/pyApp# vi app.py
from flask import Flask, render_template
import requests
import json
app = Flask(__name__)
@app.route('/')
def hello():
return render_template('hello.html' )
@app.route('/user/<userid>')
def user(userid):
userinfo = dict()
url = "http://192.168.0.105:3000/users/"+userid
try :
r = requests.get(url)
if(r.status_code == 200) :
userinfo = json.loads(r.content)
except :
pass
print(userinfo)
return render_template('user_read.html', userid=userid, userinfo=userinfo)
if __name__ == '__main__':
app.run(host='0.0.0.0', port=8000,debug=True)
~
Flask 웹어플리케이션을 실행한다.
root@client:~/pyApp/myPS# python app.py
* Serving Flask app "app" (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Running on http://0.0.0.0:8000/ (Press CTRL+C to quit)
* Restarting with stat
* Debugger is active!
* Debugger PIN: 301-791-187
웹브라우저로 확인한다
Flask 웹어플리케이션 로그에서 정보를 확인할 수 있다.
{u'CC': u'0', u'AP': u'0', u'PN': u'0', u'NM': u'LeeJinKwan', u'PW': u'1234'}
192.168.0.101 - - [01/Dec/2019 13:12:52] "GET /users/admin HTTP/1.1" 200 -
댓글목록
등록된 댓글이 없습니다.