A small customizable package to rate-limit your Flask endpoints.
Project description
Introduction
FlaskFloodgate is a small Python package that provides rate limiting functionalities for Flask endpoints.
Current Features
- Rate limit IPs.
- Restrict a certain amount of requests in a specified time window.
- Add a limit to the number of times an IP can be rate-limited (blocked).
- Punish IPs for exceeding block limit by either black-listing them or blocking them for an extended duration.
- Set the block duration based on either the first or the last blocked request.
- Set a max duration to the time a request window will be stored in the DB.
- Allow IPs to accumulate requests from past request windows.
- Allow requests with certain data.
- Blacklist and whitelist IPs during runtime.
- Can create your own custom DB Handler to manage IP data.
TODO
- Multiple DB: Implement availability for other DBs.
- Request Cooldown: A cooldown after each request.
- Adaptive blocking: Increase the window/block duration based on the severity level.
Installation
pip install FlaskFloodgate
Usage
import logging
from datetime import timedelta
from flask import Flask
from FlaskFloodgate import RateLimiter
from FlaskFloodgate.handlers import Sqlite3Handler
app = Flask(__name__)
# No need to specify all the parameters.
handler = RateLimiter(
db=MemoryHandler(),
amount=20,
time_window=timedelta(minutes=1),
block_duration=timedelta(minutes=5), # All params below this are optional.
block_limit=5,
block_exceed_duration=timedelta(days=1),
relative_block=True,
max_window_duration=timedelta(days=2),
accumulate_requests=True,
dl_data_wb=True,
logger=logging.Logger("FlaskFloodgate"),
)
handler = RateLimiter(db=db)
@app.route('/rate-limited')
@handler.rate_limited_route()
def rate_limited():
return 'Hello!', 200
if __name__ == "__main__":
app.run(host="localhost")
Documentation
For detailed functions, check out the documentation: FlaskFloodgate Documentation
Contact
You can contact me on my email: ivoscev@gmail.com
Updates
-
1.1.3
-
Fixed few bugs.
-
1.1.2
-
Improved rate-limiting logic.
-
Improved
MemoryHandler. -
Fixed bugs with
RedisHandler. -
1.1
-
Updated with a runtime terminal.
-
Moved rate limiting parameters to the
RateLimiterhandler instead of theDBHandler. -
Introduced whitelisting of IPs.
-
Introduced
RedisHandler. -
1.0.1
-
The first version (with updated PyPI README).
-
1.0
-
The first version.
Project details
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 flaskfloodgate-1.1.3.tar.gz.
File metadata
- Download URL: flaskfloodgate-1.1.3.tar.gz
- Upload date:
- Size: 3.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
91aae409932500675d674cbc3ac4134295af7137a0a826324bb388cc7cc7e6f9
|
|
| MD5 |
1117912dd308dc430441a89c63a1bdfb
|
|
| BLAKE2b-256 |
6680ded54e63c1fd7dcf5883a1bfb4a726127b012ffb41c0360a5702a036f1ed
|
File details
Details for the file flaskfloodgate-1.1.3-py3-none-any.whl.
File metadata
- Download URL: flaskfloodgate-1.1.3-py3-none-any.whl
- Upload date:
- Size: 3.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.10.2
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
ae470061a5de37eb917dde374154a841c2d60176a46c8b0fe9047efa93458782
|
|
| MD5 |
db9e567a77ea1e3fef6d5a5236d05c7e
|
|
| BLAKE2b-256 |
d1d66664ba1800c5eb3cac49409e3ce03e87bab9db47f1369a78babb77a7a798
|