已解决
python快速构建http服务
来自网友在路上 154854提问 提问时间:2023-09-28 07:39:12阅读次数: 54
最佳答案 问答题库548位专家为你答疑解惑
前提条件
pip install fastapipip install uvicorn
构建http get服务
# !/usr/bin/python
# -*- coding: utf-8 -*-
# @time : 2019/11/12 21:27
# @author : Mo
# @function: get service of fastapifrom fastapi import FastAPIapp = FastAPI()@app.get('/test/a={a}/b={b}')
def calculate(a: int=None, b: int=None):c = a + bres = {"res":c}return resif __name__ == '__main__':import uvicornuvicorn.run(app=app,host="0.0.0.0",port=8080,workers=1)
接口访问:http://127.0.0.1:8080/test/a=1/b=4
构建http post服务
# !/usr/bin/python
# -*- coding: utf-8 -*-
# @time : 2019/11/12 21:27
# @author : Mo
# @function: post service of fastapifrom pydantic import BaseModel
from fastapi import FastAPIapp = FastAPI()class Item(BaseModel):a: int = Noneb: int = None@app.post('/test')
def calculate(request_data: Item):a = request_data.ab = request_data.bc = a + bres = {"res":c}return resif __name__ == '__main__':import uvicornuvicorn.run(app=app,host="0.0.0.0",port=8080,workers=1)
只能使用postman请求
设置body为
{
“a”:1.
“b”:2
}
即可
参考连接:https://blog.csdn.net/rensihui/article/details/103038869
查看全文
99%的人还看了
相似问题
猜你感兴趣
版权申明
本文"python快速构建http服务":http://eshow365.cn/6-15084-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!
- 上一篇: 如何使用docker快速部署MinDoc文档系统
- 下一篇: 谈谈前端和后端的选择