找到下面的示例代码来理解flask和python的回调
项目> app.py
import datetime
import time
import requests # to make get and post requests
import json
from flask import Flask, Response, request, render_template # request : is not same as requests its flask request on url
app = Flask(__name__)
app.config['JSONIFY_PRETTYPRINT_REGULAR'] = True
# 3. no need to give any url mapping for callback function,
# this will get call once demo() return back to hello() at post request call.
def callbacks(response, *args, **kwargs):
print(" -- callbacks starts -- : ", datetime.datetime.now().time())
if response.status_code == 200:
#print(response.text)
print(" -- callback end -- : ", datetime.datetime.now().time());
#return response
# return Response(response, status=200) do not return response here this will cause problem when
# 2. on post request call made from hello(): resp = requests.post(......)
@app.route('/demo' , methods=['POST']) # validate request of type methods=['POST']
def demo():
print("Post request received : ", datetime.datetime.now().time())
if request.method == 'POST': # check request of type post
time.sleep(5) # some sleep time to check if callback is working
print("Post response sent to callback : ", datetime.datetime.now().time())
# return response with data received from request.data and status = 200
return Response(request.data, headers={'Content-Type': 'application/json'}, status=200)
# 1. run this file and hit url localhost:5000/ to execute ( GET request)
@app.route('/', methods=['GET'])
def hello():
print("Hello start : ", datetime.datetime.now().time())
url = 'http://jsonplaceholder.typicode.com/users'; # url will return json list
data = requests.get(
url=url,
headers={'Content-Type': 'application/json'}
) # get request to get json data from url using requests lib
# check if flask request is get
if request.method == 'GET':
# post request to another url /demo with data as a json as argument ,
# imp : hooks to link callback function once /demo url returns response callback will start execute
resp = requests.post( url = 'http://localhost:5000/demo',
headers = {'Content-Type': 'application/json'},
json = data.json(),
hooks={'response': callbacks }
)
print("Hello End : ",datetime.datetime.now().time())
# render with html template with argument as data and result
return render_template('index.html', result=resp.json(), data=data.json())
# OR
# return Response(response='OK', status=resp.status_code) # return response with 200 or 'OK'
if __name__ == '__main__':
app.run(debug=False)
创建文件 index.html 并复制以下代码
项目/模板> index.html
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>JSON OUTPUT</title>
</head>
<body>
<table border="1">
<tr><td>ID</td><td>Name</td><td>Email</td></tr>
{% for d in data %}
<tr><td>{{ d['id'] }}</td><td>{{ d['name'] }}</td><td>{{ d['email'] }}</td></tr>
{% endfor %}
</table>
<div>{{ result }}</div>
</body>
</html>
使用 cmd 运行 > python app.py