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 the max number of times an IP can be blocked by either black-listing them or block them for 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.
Suggested Features to add
- Multiple DB: Implement availability for other DBs.
- Request Cooldown: A cooldown after each request.
- Async functions: Implement async functions to prevent blocking the main thread.
- Improved logging: As of now, only INFO logs are made.
- Window status: Indicating whether a request window is 'active' or 'inactive'.
- Adaptive blocking: Increase the window/block duration based on the severity level.
- Cache layer: Implement a feature to add cache layers (Memory, Redis, etc.).
- Database connections: Implement a feature to add backup databases and automatically handle DB fail-overs.
- Request targeting: Rate-limit / Block specific requests based on certain traits (geo-blocking, malicious IPs).
- Grace Period: A grace period for new or infrequent IPs slightly exceeding the rate-limit for the first time.
Installation
pip install FlaskFloodgate
Usage
Using Sqlite3
import logging
from datetime import timedelta
from flask import Flask
from FlaskFloodgate import DefaultRateLimitHandler
from FlaskFloodgate.handlers import Sqlite3Handler
app = Flask(__name__)
def check_data(request):
return request.headers.get('key') == 'value'
# No need to specify all the parameters!
db = Sqlite3Handler(
fp="ip-data.db",
table_name='IP_Data',
amount=20, # All parameters below this are optional parameters.
time_window=timedelta(minutes=1),
block_limit=5,
block_exceed_duration='FOREVER', # Indicates that the IP will be blacklisted.
relative_block=False,
block_exceed_reset=True,
max_window_duration='FOREVER', # Indicates that none of the data will not be removed from the DB.
accumulate_requests=True,
request_data_check=check_data, # Function that checks for valid request data.
logger=logging.Logger("IP-Data")
)
handler = DefaultRateLimitHandler(db=db)
@app.route('/rate-limited')
@handler.rate_limited_route()
def rate_limited():
return 'Hello!', 200
Using Memory
import logging
from datetime import timedelta
from flask import Flask
from FlaskFloodgate import DefaultRateLimitHandler
from FlaskFloodgate.handlers import MemoryHandler
app = Flask(__name__)
# No need to specify all the parameters!
db = MemoryHandler(
amount=20, # All parameters below this are optional parameters.
time_window=timedelta(minutes=1),
block_limit=5,
block_exceed_duration=timedelta(days=7),
relative_block=False,
block_exceed_reset=True,
max_window_duration=timedelta(days=30),
accumulate_requests=True,
logger=logging.Logger("IP-Data")
)
handler = DefaultRateLimitHandler(db=db)
@app.route('/rate-limited')
@handler.rate_limited_route()
def rate_limited():
return 'Hello!', 200
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
FlaskFloodgate-1.0.1.tar.gz
(10.0 kB
view details)
Built Distribution
File details
Details for the file FlaskFloodgate-1.0.1.tar.gz
.
File metadata
- Download URL: FlaskFloodgate-1.0.1.tar.gz
- Upload date:
- Size: 10.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.2 CPython/3.10.2
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 45ce9515175ed817f1ffcdbf08e4fd8ef8770752b722b39111c165c224912caf |
|
MD5 | a44b5f4bd26c491ca456c9bd2517e475 |
|
BLAKE2b-256 | fe8842b146bdae64aeaa79b29763c30f405b74537895e1844b083b3fd69df107 |
File details
Details for the file FlaskFloodgate-1.0.1-py3-none-any.whl
.
File metadata
- Download URL: FlaskFloodgate-1.0.1-py3-none-any.whl
- Upload date:
- Size: 9.6 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 | 878df729b78d8af75698a6b4066d6ac2fc18ad7729670b39a6ec6dab81628193 |
|
MD5 | 6c5e467bf984243294ee7486536fa961 |
|
BLAKE2b-256 | bd3c883f4f37b6016692820e02899c9f6fdeac0293f61dedd69650d28abe7908 |