An extension that provides rate limiting for Flask routes.
Project description
Flask_Limit
An extension that provides rate limiting for Flask routes.
Installation
The easiest way to install this is through pip.
pip install Flask_Limit
Configuration
This extension depends on two configuration parameters RATELIMITE_LIMIT and RATELIMIT_PERIOD. If this parameters are not set, default values of 10 and 20 are used respectively, which represents the number of allowed requests(limit) within a given time(period).
Basic Usage
The easiest way to rate limit the entire application is limit the application's before request method. The rate_limit decorator can be called with or without the litmit and period paramters. If this parameters are not provided, the values are gotten from the application's configuration. In the example below, after rate limiting the before_request method, a get request to /greet/ will show from the response headers that the rate limiting is working.
In-Memory example
This example uses MemRateLimit
which is the default Rate-Limiter.
from flask import Flask, g
from flask_limit import RateLimiter
class Config:
RATELIMIT_LIMIT = 10 # Number of allowed requests.
RATELIMIT_PERIOD = 30 # Period in seconds.
app = Flask(__name__)
app.config.from_object(Config)
limiter = RateLimiter(app)
@app.before_request
@limiter.rate_limit
def before_request():
pass
@app.after_request
def after_request(rv):
headers = getattr(g, 'headers', {})
rv.headers.extend(headers)
return rv
@app.route('/greet/<name>')
def greet(name):
return f'Hello {name}!'
if __name__ == '__main__':
app.run()
Redis example
This example uses RedisRateLimit
as the Rate-Limiter.
Remember to set the REDIS_URL
parameter in your configuration
from flask import Flask, g
from flask_limit import RateLimiter
class Config:
RATELIMIT_LIMIT = 10
RATELIMIT_PERIOD = 30
REDIS_URL = "redis://localhost:6379/0"
app = Flask(__name__)
app.config.from_object(Config)
limiter = RateLimiter(app, limiter="redis")
@app.before_request
@limiter.rate_limit
def before_request():
pass
@app.after_request
def after_request(rv):
headers = getattr(g, "headers", {})
rv.headers.extend(headers)
return rv
@app.route("/greet/<name>")
def greet(name):
return f"Hello {name}!"
if __name__ == "__main__":
app.run()
Complex example
More than one route can be rate limited.
from flask import Flask, g
from flask_limit import RateLimiter
class Config:
RATELIMIT_LIMIT = 10
RATELIMIT_PERIOD = 30
app = Flask(__name__)
app.config.from_object(Config)
limiter = RateLimiter(app)
@app.before_request
@limiter.rate_limit
def before_request():
pass
@app.after_request
def after_request(rv):
headers = getattr(g, 'headers', {})
rv.headers.extend(headers)
return rv
@app.route('/greet/<name>')
def greet(name):
return f'Hello {name}!'
@app.route('/get-auth-token')
@limiter.rate_limit(limit=1, period=600) # one call per 10 minute period
def get_auth_token():
return {'token': '<auth-token>'}
if __name__ == '__main__':
app.run()
Tests
python -m pip install --upgrade pip
pip install tox tox-gh-actions
tox
Proof
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
File details
Details for the file Flask_Limit-2.0.5.tar.gz
.
File metadata
- Download URL: Flask_Limit-2.0.5.tar.gz
- Upload date:
- Size: 698.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.13
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d885614eac17be904a8be41d8b8020b2dd0d8ffb4390f045214a0a447ee05986 |
|
MD5 | 679f808536153b6d6bc5d4115c6d6f60 |
|
BLAKE2b-256 | 15664b6f4559509d8b1b2122461f0edc87e6a0b08b829148b9a1e80758ee10d9 |