FASTAPI接口服务
安装fastapi:
pip install fastapi
pip install uvicorn
方式一:
httpserver1.py
# coding=utf-8
# #!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2021/4/16 17:19
# @Author : LQ
# @FileName: httpServer1.py
# @Software: PyCharm
from pydantic import BaseModel
from fastapi import FastAPI, Request
from starlette.responses import HTMLResponse
app = FastAPI()
@app.get('/test/a={a}/b={b}')
def calculate(a: int = None, b: int = None):
c = a + b
res = {
"res": c}
return res
if __name__ == '__main__':
import uvicorn
uvicorn.run(app=app,
host="0.0.0.0",
port=8081,
workers=1)
运行httpserver.py
POSTMAN或浏览器输入:http://127.0.0.1:8081/test/a=1/b=2
postman:
浏览器:
方式二:
httpserver2.py
# coding=utf-8
# #!/usr/bin/python3
# -*- coding: utf-8 -*-
# @Time : 2021/4/16 17:19
# @Author : LQ
# @FileName: httpServer2.py
# @Software: PyCharm
from pydantic import BaseModel
from fastapi import FastAPI, Request
from starlette.responses import