A small customizable package to rate-limit your Flask endpoints.
Project description
Introduction
Flask-IP-Floodgate 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.
- Improved logging: As of now, only INFO logs are made.
- 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, # All parameters below this are optional.
time_window=timedelta(minutes=1)
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.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.tar.gz
.
File metadata
- Download URL: FlaskFloodgate-1.1.tar.gz
- Upload date:
- Size: 10.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 97cc06058a640bafa8335dec363989378b361de8ab4e7917fd15b2cea10ef071 |
|
MD5 | bdbd931124c609549685d0d5928ed28f |
|
BLAKE2b-256 | 943e803881c17c8c98f54176a3d3ebd524156b1aec4bd724813994c2ed20e77b |
File details
Details for the file FlaskFloodgate-1.1-py3-none-any.whl
.
File metadata
- Download URL: FlaskFloodgate-1.1-py3-none-any.whl
- Upload date:
- Size: 10.2 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 | 50724332b1de1df5bba2968342fb2eab5ba571f8208e0aba4554af0cb22eb30c |
|
MD5 | ea5b127f47f09b4b183ed6cd85bd783e |
|
BLAKE2b-256 | 3121de159eed1af6c4eaffd3a1525d9eacc8fa0e484e54a11926ed345807793a |