国庆节10月1日
--


Web 网站的三个组成部分与 FastAPI 的四步入门用法、两种启动方式,以及 uvicorn 是什么

Web:全球广域网,也称为万维网(www,World Wide Web),能够通过浏览器访问到的网站。
一个 Web 网站由三个核心部分组成:
| 部分 | 作用 | 用什么做 |
|---|---|---|
| 前端程序 | 界面展示 | HTML(结构)+ CSS(表现)+ JavaScript(行为) |
| 服务端程序 | 业务逻辑处理 | Python 里可用 Django / Flask / FastAPI |
| 数据库 | 数据存储和管理 | MySQL 等(本章案例用 JSON 文件代替) |
前端三件套的分工在第 4 章(爬虫)里已经见过:HTML 管结构、CSS 管样式、JS 管交互。这里要写的是服务端。
FastAPI 是一个现代、快速、高性能的 Web 框架,用于基于标准的 Python 类型提示构建 API 接口服务。
API 接口:应用程序编程接口(Application Programming Interface),就是对外提供的功能入口,供别人来调用(比如天气查询的 API 接口)。一次调用的过程就是:客户端发 HTTP 请求 → 服务端返回 HTTP 响应。
1from fastapi import FastAPI # 1. 导入 FastAPI2
3app = FastAPI() # 2. 创建 FastAPI 实例4
5@app.get("/") # 3. 创建路径操作函数,定义访问路径6def root():7 return {"message": "Hello World"}8
9@app.get("/users")10def get_users():11 return [12 {"id": 1, "name": "张三"},13 {"id": 2, "name": "李四"},14 {"id": 3, "name": "王五"},15 ]16
17if __name__ == "__main__": # 4. 运行 FastAPI 服务18 import uvicorn19 uvicorn.run(app, host="0.0.0.0", port=8000)| 步骤 | 代码 |
|---|---|
| 1. 导入 | from fastapi import FastAPI |
| 2. 创建实例 | app = FastAPI() |
| 3. 定义接口 | @app.get("/路径") + 函数(函数返回值就是接口返回的数据) |
| 4. 运行服务 | 命令行 fastapi dev "xxxx.py" 或 uvicorn xxxx:app --reload;也可以写在代码里 uvicorn.run(...) |
1# 方式一:FastAPI 官方 CLI2fastapi dev "main.py"3
4# 方式二:直接用 uvicorn(xxxx:app 表示"文件:FastAPI实例名")5uvicorn main:app --reload| 名词 | 说明 |
|---|---|
| uvicorn | 专门为现代 Python Web 框架(FastAPI、Starlette)设计的高性能服务器 |
main:app | 文件 main.py 里的 app 对象 |
--reload | 改代码后自动重启(开发时用) |
启动后浏览器打开 http://127.0.0.1:8000 就能看到接口返回的 JSON;FastAPI 还自带交互式接口文档,打开 /docs 就能直接在网页上试接口——这也是它比 Django/Flask 更适合做 API 的地方。
fastapi dev "main.py" 或 uvicorn main:app --reload 2-1 第一个接口
写一个 FastAPI 程序:/ 返回 {"message": "Hello World"},/users 返回 3 个用户的列表。
一级 · 思路:创建实例 → 用装饰器把函数”挂”到路径上
二级 · 方法:app = FastAPI() / @app.get("/")
三级 · 骨架:函数返回字典或列表,FastAPI 会自动转成 JSON
1# 2-12from fastapi import FastAPI3
4app = FastAPI(title="入门练习")5
6USERS = [7 {"id": 1, "name": "张三"},8 {"id": 2, "name": "李四"},9 {"id": 3, "name": "王五"},10]11
12@app.get("/")13def root():14 return {"message": "Hello World"}15
16@app.get("/users")17def get_users():18 return USERS 2-2 带路径参数的接口
再加一个 /users/{user_id} 接口:根据路径里的编号返回对应用户(编号不存在就返回提示)。
一级 · 思路:路径里的花括号就是”路径参数”,要写进函数形参
二级 · 方法:@app.get("/users/{user_id}") + def get_user(user_id: int):
三级 · 骨架:user_id: int 会让 FastAPI 自动把字符串转成整数
1# 2-22@app.get("/users/{user_id}")3def get_user(user_id: int):4 for user in USERS:5 if user["id"] == user_id:6 return user7 return {"message": "用户不存在"}2-3 用测试客户端验证 不启动服务器,用 FastAPI 自带的测试客户端直接请求上面几个接口,打印状态码和返回内容。
一级 · 思路:像写单元测试一样调接口,不用真的开服务器
二级 · 方法:from fastapi.testclient import TestClient / client = TestClient(app)
三级 · 骨架:print(response.status_code, response.json())
1# 2-3:不启动服务器也能测2from fastapi.testclient import TestClient3
4if __name__ == "__main__":5 client = TestClient(app)6 for path in ["/", "/users", "/users/2", "/users/99"]:7 resp = client.get(path)8 print(path, resp.status_code, resp.json())9
10# 真正启动服务时用:11# fastapi dev main.py 或 uvicorn main:app --reload如果你喜欢,那么欢迎来到我的世界!
了解更多暂未播放



"生活不是等待暴风雨过去,而是学会在雨中跳舞。"—— 塞维涅夫人


