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.
from flask import Flask, g
from flask_limit import RateLimiter
class Config:
RATELIMITE_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}!'
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:
RATELIMITE_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()
Proof
Credit
Credit to Miguel Grinberg for his exception work on rate limiting, from which this extension is based on.
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file Flask_Limit-1.0.1.tar.gz.
File metadata
- Download URL: Flask_Limit-1.0.1.tar.gz
- Upload date:
- Size: 5.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.7.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
0d4d94161dc1a2eace37ddf4aba905ca839ba15c13d8e2fe740846833c475f35
|
|
| MD5 |
e883dcc4b37aa31dbcd15682b18228c6
|
|
| BLAKE2b-256 |
4406f3462497365a7f0143f5a1f07e641cc6a37868a66d31ae8b2786a96dfe38
|
File details
Details for the file Flask_Limit-1.0.1-py3-none-any.whl.
File metadata
- Download URL: Flask_Limit-1.0.1-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/3.1.1 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.41.0 CPython/3.7.5
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7d0337c170df7d1be13536036a8ea56baa9c71afd26a74590620c9604c2fc7f7
|
|
| MD5 |
ec94d22e79bd346f7d3ec667e95512de
|
|
| BLAKE2b-256 |
5908ec43cc28d3e09e02174e1c403df2c58d8c40878f638ef6e2acffdce498bf
|