Flask发布post读写json格式参数进行模型调用
安装Flask
venv\Scripts\activate
安装依赖pip install -r requirements.txt
venv\Scripts\activate
Flask==2.0.1 Werkzeug==2.0.1 psycopg2-binary==2.9.3 SQLAlchemy==1.4.36 Flask-SQLAlchemy==2.5.1 python-dotenv==0.19.0 geoalchemy2==0.10.0 alembic==1.7.0 flask-migrate==3.1.0
学习参考资料:
Flask模板引擎:https://b23.tv/KBIrrOH
代码内容:
import os.path from flask import Flask, request, render_template from werkzeug.utils import send_file from flask import send_file app = Flask(__name__) class InvalidParaException(Exception): pass @app.route("/") def index(): return "Server is running..." @app.route("/get") def get(): name= request.args.get("name", "Jack") if name.strip() == "": raise InvalidParaException("name字段不能为空..") if name.strip() == "123": raise InvalidParaException("name字段不能为123..") return f'name is {name}' # http://127.0.0.1:5000/get?name=Tom @app.route("/post", methods=['POST']) def post(): json_data= request.get_json() name = json_data.get("name", "Jack") return f'name is {name}' @app.errorhandler(Exception) def page_not_found(error): return f'出错了。全局错误 {error}' @app.errorhandler(InvalidParaException) def page_not_found(error): return f'出错了。无效参数 {error}' # 读取文件 @app.route('/read-text') def read_text(): filename = request.args.get("filename") if filename == "": return "请传递filename参数!" filepath = f"text/{filename}" if not os.path.exists(filepath): return f'文件[{filename}]不存在' # http://127.0.0.1:5000/read-text?filename=唐诗.txt return send_file('text/唐诗.txt') # return send_file('./sessions.csv',mimetype='csv',as_attachment=True, # download_name='sessions.csv', environ=request.environ) from markupsafe import escape @app.route('/dynamic-route/<name>/yyds') def dynamic_route(name): # show the user profile for that user return f'name is {name}' # http://127.0.0.1:5000/dynamic-route/xiaoming/yyds # flask框架下使用模板 @app.route('/welcome') def welcome(): return render_template("welcome.html",name = "xiaoming") # http://127.0.0.1:5000/welcome app.run()

结合高空伞兵高级模型
from flask import Flask, request, jsonify # from sympy.codegen import Print # import FlightModel.Flightlandingpoint from FlightModel.Flightlandingpoint import cal_fLP # url = "http://localhost:5000/api-endpoint" # data = { # "username": "tech_enthusiast", # "preferences": { # "theme": "dark", # "notifications": True # } # } # import requests # response = requests.post(url, json=data) # print(response.json()) app = Flask(__name__) @app.route('/post', methods=['POST']) def post_json(): try: # 1、获取请求中的 JSON 数据 data = request.get_json() if data is None: return jsonify({ "error": "No JSON data provided" }), 400 # 2. 验证数据完整性 if (not data or 'flightLandPoint' not in data or 'v_front' not in data or 'h' not in data or 'angle' not in data): return jsonify({ "status": "error", "code": 400, "message": "缺少必要参数 (flightlandPoint or v_front or h or angle)" }), 400 # print('输出:', data) # name = data.get('flightLandPoint') # print(name) # 读取特定参数 flightLandPoint = data.get('flightLandPoint')['geometry']['coordinates'] v_front = data.get('v_front') h = data.get('h') angle = data.get('angle') # 进行一些处理,这里只是简单示例 # result = { # "code": "200", # "message": "成功", # "features": [ # { # "geometry": { # "coordinates": [ # 103.48775167660897, # 30.923409633492632 # ], # "type": "Point" # }, # "type": "Feature", # "properties": { # "flightLandPoint": flightLandPoint, # "v_front": v_front, # "h": h, # "angle": angle # } # }] # } # [L,B] = flightLandPoint cor, t = cal_fLP(flightLandPoint, v_front, h, angle) result = { "code": "200", "message": "成功", "features": [ { "geometry": { "coordinates": cor, "type": "Point" }, "type": "Feature", "properties": { "time": t } }] } return jsonify(result), 200 except Exception as e: return jsonify({ "status": "error", "code": 500, "message": f"Server internal error: {str(e)}" }), 500 if __name__ == '__main__': app.run(debug=True) # curl -X POST http://127.0.0.1:5000/process -H "Content-Type: application/json" -d '{"flightlandpoint":{"type":"Feature","geometry":{"type":"Point","coordinates":[103.49204567902446,30.926804485433806]},"properties":{}},"v_front":800,"h":1200,"angle":130}' #postwoman #POST:http://127.0.0.1:5000/post # 自定义格式:application/json # 参数: # { # "flightLandPoint": { # "type": "Feature", # "geometry": { # "type": "Point", # "coordinates": [ # 103.49204567902446, # 30.926804485433806 # ] # }, # "properties": {} # }, # "v_front": 800, # "h": 1200, # "angle": 130 # } # 发送结果 # { # "code": "200", # -"features": [ # -{ # -"geometry": { # -"coordinates": [ # 103.49181888, # 30.92717512 # ], # "type": "Point" # }, # -"properties": { # "time": 184.4799999999772 # }, # "type": "Feature" # } # ], # "message": "成功" # }
POSTWOMAN测试


浙公网安备 33010602011771号