FastAPI的最简单实例

前言

FastAPI越来越流行,好处多多,慢慢会成为Web后端的主流。我初步尝试了一下。

一、基本代码

代码如下:

from fastapi import FastAPI

创建应用实例

app = FastAPI()

========== 定义一个路由 ==========

@app.get("/") # GET 请求

def read_root():

return {"message": "Hello, FastAPI!"}

@app.get("/hello/{name}") # 动态路由参数

def say_hello(name: str):

return {"message": f"你好,{name}!欢迎使用 FastAPI!"}

@app.post("/sum") # POST 请求示例

def calc_sum(data: dict):

a = data.get("a", 0)

b = data.get("b", 0)

return {"a": a, "b": b, "sum": a + b}

二、启动运行

三、访问效果

四、Post请求示例

输入参数:

运行结果如下:

相关推荐
LK_074 天前
【FastAPI】基础篇 01|开始之前:API、HTTP 与 FastAPI 到底是什么
网络协议·http·fastapi
一枕槐安4879 天前
Qwen 通义千问本地部署与推理服务封装
fastapi·qwen·vllm·流式推理·openai兼容api·awq量化
在努力的前端小白9 天前
基于FastAPI+LangChain+LangGraph+Agent+多模态的AI Agent 有渔渔具网站
人工智能·langchain·vue·fastapi
sun_weitao9 天前
用 FastAPI + MQTT 做一个 ESP32最小化跑通示例
中间件·vue·fastapi
峯58010 天前
一文学习FastAPI
学习·fastapi
TheBestRucy12 天前
从零构建企业级“中文法律智能问答系统”:FastAPI+Milvus+混合检索+真流式RAG实战
fastapi·milvus
卡提西亚12 天前
FastAPI基础入门
python·fastapi
Paper-Tiger13 天前
手搓 FastAPI + Redis + 流式 RAG 实录
fastapi
像风一样自由202013 天前
2.如何用FastAPI把一个Python算法封装成HTTP服务
python·算法·fastapi
狗凯之家源码网13 天前
iOS C2 V2 与 Python FastAPI 全链路远控系统深度评测
python·ios·fastapi