Web应用-FastAPI

FastAPI的作用:把函数 / 模型包装成HTTP 接口,供前端或其他服务调用。(和Java中Spring Boot写接口的作用是一样的)

官方文档:FastAPI官方文档

安装:

bash 复制代码
 pip intsall fastapi
python 复制代码
pip install "fastapi[standard]"

使用步骤:

  1. 导入FastAPI
python 复制代码
from fastapi import FastAPI
  1. 创建FastAPI实例对象

  2. 创建路径操作函数,定义访问路径

python 复制代码
app = FastAPI()


@app.get("/")
def test1():
    return ("hello world!")
  1. 运行FastAPI服务

运行命令:

  • 方式一(FastAPI CLI 命令):

    python 复制代码
    fastapi dev "xxxx.py"
  • 方式二(uvicorn 直接运行):

    python 复制代码
    # 启动服务
    if __name__ == "__main__":
        import uvicorn
        uvicorn.run(app,host="127.0.0.1",port=8000)
相关推荐
阿童木写作19 分钟前
Python实现亚马逊商品图批量智能抠图教程
人工智能·python
axinawang32 分钟前
第24课:while循环的应用
python
三亚兴嘉装饰40 分钟前
三亚服装店装修
python
小柯南敲键盘1 小时前
速卖通AI图片翻译API集成实战
人工智能·python
tqs_123452 小时前
Agent智能体+Skill插件引擎+Milvus混合检索 技术沉淀
java·python·milvus
眼泪划过的星空2 小时前
LangChain 两大基础提示词模板:PromptTemplate 与 ChatPromptTemplate 详解
人工智能·python·langchain
狗都不学爬虫_2 小时前
AI逆向 - 99aq中心滑块验证+登录(wasm纯算)
爬虫·python·网络爬虫
天才测试猿2 小时前
实例介绍:Unittest框架及自动化测试实现流程
自动化测试·软件测试·python·selenium·测试工具·职场和发展·测试用例
veminhe3 小时前
元数据索引有关的错
人工智能·python
一次旅行3 小时前
AutoAWQ完整实战:MIT激活感知AWQ量化,模型显存减半、推理提速且精度无损
人工智能·python·算法