4.2 服務端部署案例講解
本節要點
- 服務端部署實例(定時框架)
- 服務端部署案例(前端事件)
- 服務端部署案例(Webhook)
課前準備
無
課程內容
後端應用實例(定時框架)
import json
from apscheduler.schedulers.blocking import BlockingScheduler
import requests
def get_jdy_data():
url = "https://www.jiandaoyun.com/api/v1/app/5afe2461743ef87fde26a894/entry/5c12198c99b0d10dc49edfff/data"
payload = {}
headers = {
'Authorization': 'Bearer ????????????????????'
}
response = requests.request("POST", url, headers=headers, data = payload)
print(response.text)
if __name__ == '__main__':
scheduler = BlockingScheduler()
scheduler.add_job(get_jdy_data, 'cron', day_of_week='*' ,hour='14', minute='05', second='00')
scheduler.start()
後端應用實例(前端事件)
類型 | 參數 | 資料格式 |
傳入參數 | x | JSON |
返回參數 | y | JSON |
from flask import Flask,request
import json
app = Flask(__name__)
def calc(x):
y = x + 10
return y
@app.route('/test/' ,methods=['POST'])
def hello_world():
x = json.loads(request.data)['x']
y = calc(int(x))
return json.dumps({'y':y})
if __name__ == '__main__':
app.run(host='0.0.0.0',port=3100)
服務端部署案例(Webhook)
from flask import Flask,request
import json
import threading
app = Flask(__name__)
def calc(x):
y = x + 10
print(y)
@app.route('/test/' ,methods=['POST'])
def hello_world():
x = json.loads(request.data)['data']['_widget_1586252392790']
threading.Thread(target=calc,args=(int(x),)).start()
return 'success',200
if __name__ == '__main__':
app.run(host='0.0.0.0',port=3100)
課後作業
編程題作業
作業一:使用前端事件功能實現體積計算(v=xyz)(3分)截圖Python程式碼 提示:在表單中新增4個控件(x,y,z,v),輸入x,y,z後(z為前端事件觸發欄位) 觸發前端事件調用API計算並回寫v
作業二:使用Webhook實現體積計算(v=xyz)(3分)截圖Python程式碼 提示:在表單中新增4個控件(x,y,z,v),輸入x,y,z後送出資料觸發Webhook,推播至ECS計算體積,計算v後使用資料API回寫至對應的資料
from flask import Flask,request
import json
import threading
import requests
app = Flask(__name__)
def post_to_jdy(v,data_id):
pass
def calc(x,y,z,data_id):
pass
@app.route('/test/' ,methods=['POST'])
def hello_world():
x = json.loads(request.data)['data']['_widget_1586252719049']
y = json.loads(request.data)['data']['_widget_1586252719064']
z = json.loads(request.data)['data']['_widget_1586252719079']
data_id = json.loads(request.data)['data']['_id']
threading.Thread(target=calc,args=(float(x),float(y),float(z),data_id,)).start()
return 'success',200
if __name__ == '__main__':
app.run(host='0.0.0.0',port=3100)