使用 Flask-Limiter 限流请求
应用逐路由和全局速率限制
使用 Flask-Limiter 限流请求 是 CoddyKit 上的免费 Flask Academy 课时。 这是第 1 节课,共 4 节。 你可以在下方免费阅读本课时的完整内容 — 然后在浏览器中使用内置代码编辑器和全天候 AI 导师进行实践。 这是 Flask Academy 学习路径的一部分,你的进度在网页和 CoddyKit 应用中同步。 Flask Academy 课程共包含 4 节课。
本课时的部分内容尚未翻译,以英文显示。
Why Throttle At All
Without limits, one client can flood your API and starve everyone else. Rate limiting caps how often a caller may hit your routes.
Meet Flask-Limiter
The go-to tool is the Flask-Limiter extension. It watches incoming requests and rejects callers who go over their allowance.
Install It
You add it like any other extension with pip. One package gives you decorators and config for limiting traffic.
pip install Flask-LimiterIdentify the Caller
A limiter needs a key to count by. The usual choice is the client IP via the built-in get_remote_address helper.
from flask_limiter.util import get_remote_addressCreate the Limiter
You build a Limiter and bind it to your app. The key function decides who each request counts against.
limiter = Limiter(get_remote_address, app=app)Set a Default Limit
A default_limits list applies one cap to every route at once. It is your safety net for the whole app.
limiter = Limiter(get_remote_address, app=app,
default_limits=["200 per day", "50 per hour"])Per-Route Limits
Tighten a single endpoint with the @limiter.limit decorator. This route gets its own stricter rule.
@app.route("/login")
@limiter.limit("5 per minute")
def login():
...Reading the Limit String
The string reads like plain English. 5 per minute means a caller may hit that route five times each minute.
"5 per minute"What 429 Means
When a caller exceeds the cap, Flask-Limiter returns 429 Too Many Requests. It is the polite way to say slow down.
Exempt a Route
Some endpoints, like a health check, should never be throttled. Mark them with @limiter.exempt to skip all limits.
@app.route("/health")
@limiter.exempt
def health():
return "ok"Use a Shared Backend
In production you run many app processes, so counts must live in a shared storage_uri like Redis, not memory.
Limiter(get_remote_address, app=app,
storage_uri="redis://localhost:6379")Quick Check
Pick the status a throttled client receives.
Recap
You added Flask-Limiter, keyed limits by IP, set default and per-route caps, exempted health checks, and learned the 429 reply. Great job!
常见问题解答
「使用 Flask-Limiter 限流请求」课时是免费的吗?
是的 — 「使用 Flask-Limiter 限流请求」的完整文本可在网页上免费阅读。要进行交互式练习(内置代码编辑器和全天候 AI 导师)并解锁 Flask Academy 课程的其余内容,请升级到 CoddyKit PRO。 Flask Academy 课程共包含 4 节课。
「使用 Flask-Limiter 限流请求」这节课中我会学到什么?
应用逐路由和全局速率限制 你通过在浏览器中直接运行的动手代码来练习 Flask Academy,全天候 AI 导师会在你学习这节课的过程中回答你的问题。
学习 Flask Academy 需要有经验吗?
无需任何先前经验。CoddyKit 上的 Flask Academy 课程适合初学者到高级学习者,你可以从这里开始或从头开始,按照自己的节奏学习。 这是第 1 节课,共 4 节。
「使用 Flask-Limiter 限流请求」课时需要多长时间?
大多数 CoddyKit 课程大约需要 5–10 分钟。每节课都很精短且互动,所以你能稳步进步,并在网页和应用中从离开的地方继续。
我能在这节 Flask Academy 课中编写并运行代码吗?
能。每节 Flask Academy 课都包含内置代码编辑器,你可以在浏览器中直接编写并运行真实代码,并获得即时 AI 反馈 — 无需本地设置。
此课程中的所有课时
- 使用 Flask-Limiter 限流请求
- 为浏览器客户端配置 CORS
- 安全标头与 HTTPS
- 验证输入以阻止注入