admin 管理员组

文章数量: 1087649

python实现api接口

import sys
sys.path.append("./src") # 把代码路径加入进去防止引用自定义模块报错from fastapi import FastAPI
from src.DiffProcess import Run # 写的程序入口res = Run() # 程序返回结果得到app = FastApi()@app.get("./") # get请求方法可以用浏览器直接请求
def GetResult(subnetId:str):return res[subnetId] # 显示结果一定要是字典形式

在终端中运行:

main:app --reload

或者直接在主程序中加:

if __name__ == "__main__":import uvicornuvicorn.run(app = app,host="0.0.0.0",port=8000,workers=1)

运行即可

在浏览器中输入:

http://127.0.0.1:8000/?subnetId=71-1

即可得到返回结果,?标识参数调用,直接传入到@app装饰的函数之中

本文标签: python实现api接口