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.
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),
block_limit=5,
block_exceed_duration=timedelta(days=1),
relative_block=True,
block_exceed_reset=True,
max_window_duration=timedelta(days=2),
accumulate_requests=True,
dl_data_wb=True,
logger=logging.Logger("FlaskFloodgate"),
export_dir=os.getcwd()
)
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.2
-
Improved rate-limiting logic.
-
Improved
MemoryHandler
. -
Fixed bugs with
RedisHandler
. -
1.1
-
Updated with a runtime terminal.
-
Moved rate limiting parameters to the
RateLimiter
handler 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
File details
Details for the file flaskfloodgate-1.1.2.tar.gz
.
File metadata
- Download URL: flaskfloodgate-1.1.2.tar.gz
- Upload date:
- Size: 11.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 7dd0667b21da0b84154297053af5ed8465e5c4545be17a9bdf82b462a36b743a |
|
MD5 | ca63830b4b0c453b349a004c305bb014 |
|
BLAKE2b-256 | c292566620287d9557561742b06c705ba8c66e78562c202526040cb0aa426f88 |
File details
Details for the file FlaskFloodgate-1.1.2-py3-none-any.whl
.
File metadata
- Download URL: FlaskFloodgate-1.1.2-py3-none-any.whl
- Upload date:
- Size: 10.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 394228bb0392a876e7860b37c8360229fd5d3ac60f875fb8667fd032f1790920 |
|
MD5 | 77987f69bcea6b0dfcadfe1c479902fc |
|
BLAKE2b-256 | a9f8e0b4e884ca380f5296ae01bc0e17c108376f02ab098aadb142503aa99b87 |