Python Fast API
Fast API 配置项目的python环境 可以 install python 所需的所有环境,不用一个一个的 pip install pip freeze > piplist.txt pip install -r piplist.txt 创建 fastapi from fastapi import FastAPI app = FastAPI() app.add_middleware( CORSMiddleware, allow_origins=["*"], allow_credentials=True, allow_methods=["*"], allow_headers=['Content-Type','application/xml'], ) @app.get("/gettest") async def gettest(): return {"hello world"} @app.post("/posttest") async def posttest(json_data: Dict): # 传进 JSON_DATA 注意 尾行不留空格 # { # "investdate": "2022-03-04", # "weights":[ # { # "id": "AMD", # "weight": -0.344 # }, # { # "id": "AMZN", # "weight": 0.699 # }, # { # "id": "ZM", # "weight": 0.499 # } # ] # } # 如何处理 json 数据 investdate = json_data['investdate'] weight = json_data['weights'] # 如何把 json 数组转化成 dict input_weght = dict() for data in weight: id = data["id"] weight = data["weight"] input_weght[id] = weight # 接口里定义的函数 逻辑部分 def inquirie_for_customer(investdate, weight: dict): return return_json # 如何把结果传回去 result = inquirie_for_customer(investdate,input_weght) return JSONResponse(status_code=200, content=result) 运行 fastapi py fastapi_server.py ...