静态文件在static目录下可以访问
# -*- coding:utf-8 -*-
import json
import random
from flask import Flask, request
from flask_cors import CORS
# # 跨域支持
app = Flask(__name__, template_folder='templates', static_folder='static', static_url_path='/')
CORS(app, supports_credentials=True, methods=["GET", "HEAD", "POST", "OPTIONS", "PUT", "PATCH", "DELETE"], allow_headers='*')
host = '0.0.0.0'
post = '9999'
# # 测试接口
@app.route('/test', methods=['GET', 'POST'])
def test():
res_dict = {
"ratio": random.random() / 10,
"result_code": 200,
"res_msg": 'ok'
}
return json.dumps(res_dict, ensure_ascii=False)
# # 访问接口
@app.route('/api/XXX', methods=['POST'])
def get_XXX_main():
if request.method == "POST":
""" 读取入参的两种方式
# # [1]
# json_data = json.loads(request.get_data().decode('utf-8'))
# txt_message = json_data.get('txt_message')
# # [2]
# txt_message = request.form.get('txt_message')
# print("txt_message=", txt_message)
"""
txt_message = request.form.get('txt_message')
print("txt_message=", txt_message)
# # 处理过程
if len(txt_message) != 0:
returnCase, returnData = xxxx(txt_message)
# # 处理成功
if returnCase == "0000":
res_dict = {
"inputQuestion": txt_message,
"returnCode": returnCase,
"returnData": returnData,
"returnMessage": "right! 666,666"
}
else:
res_dict = {
"inputQuestion": txt_message,
"returnCode": returnCase,
"returnData": returnData,
"returnMessage": "some question! 555,555"
}
# # 错误
else:
res_dict = {
"inputQuestion": txt_message,
"returnCode": "1111",
"returnData": '',
"returnMessage": "The question input is empty! emmmmmm"
}
return json.dumps(res_dict, ensure_ascii=False)
if __name__ == '__main__':
app.run(host=host, port=post, threaded=True, debug=False)