Python Web 框架 FastAPI 快速上手
前言 Flask 时代写 API 要自己造的轮子——参数校验、文档、类型提示——FastAPI 一次全给:写函数签名,送你文档和校验。这篇从零到部署走一遍。 一、五分钟起步 pip install "fastapi[standard]" -i https://mirrors.aliyun.com/pypi/simple/ # main.py from fastapi import FastAPI app = FastAPI(title="运维平台 API", version="1.0.0") @app.get("/healthz") def healthz(): return {"status": "ok"} @app.get("/hello/{name}") def hello(name: str): return {"message": f"hello, {name}"} fastapi dev main.py # 开发模式, 改代码热重载 # 打开 http://localhost:8000/docs ← 自动交互式文档(Swagger UI) # 打开 http://localhost:8000/redoc ← 另一套文档样式 类型注解就是功能:name: str 声明后,框架自动解析路径参数、校验类型、生成文档。 ...