A (very) simple banning & rate limiting extension for Flask.
Project description
flask-gatekeeper
A simple banning & rate limiting extension for Flask.
It's not meant to be a replacement for other, more complex banning & rate limiting modules like flask-Limiter
or flask-ipban
.
It has the following specificities:
- no dependencies,
- quite fast due to the use of
collections.deque
, - in-memory storage (no persistence across restarts).
Full documentation can be found here: https://k0rventen.github.io/flask-gatekeeper/
Getting started
Install
pip install flask-gatekeeper
Sample usage
Here is a demo app showing the main capabilities of flask-gatekeeper :
# import flask-gatekeeper along flask
from flask import Flask
from flask_gatekeeper import GateKeeper
app = Flask(__name__)
gk = GateKeeper(app, # or use .init_app(app) later
ip_header="x-my-ip", # optionnal header to use for the client IP (e.g if using a reverse proxy)
ban_rule={"count":3,"window":10,"duration":600}, # 3 reports in a 10s window will ban for 600s
rate_limit_rules=[{"count":20,"window":1},{"count":100,"window":10}], # rate limiting will be applied if over 20 requests in 1s or 100 requests in 10s
excluded_methods=["HEAD"]) # do not add HEAD requests to the tally
# By default, all routes will use the rate limiting we defined above:
@app.route("/ping") # this route is rate limited by the global rule
def ping():
return "ok",200
@app.route("/login") # also rate limited by the global rule
def login():
if request.json.get("password") == "password":
return token,200
else:
gk.report() # report the request's IP, after 3 reports in this case the IP will be banned
return "bad password",401
# we can specify different rate limiting rules using decorators
@app.route("/global_plus_specific")
@gk.specific(rate_limit_rules=[{"count":1,"window":2}]) # add another rate limit on top of the global one (to avoid bursting for example)
def specific():
return "ok",200
@app.route("/standalone")
@gk.specific(rate_limit_rules=[{"count":10,"window":3600}],standalone=True) # rate limited only by this rule
def standalone():
return "ok",200
@app.route("/bypass")
@gk.bypass # do not apply anything on that route
def bypass():
return "ok",200
app.run("127.0.0.1",5000)
Copy that in a file or your REPL, then try the various endpoints.
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 flask_gatekeeper-1.0.0.tar.gz
.
File metadata
- Download URL: flask_gatekeeper-1.0.0.tar.gz
- Upload date:
- Size: 7.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 262c1db78aae2c8908e4fd4bbf8ed2cfe0a1527299e92added75ebfb106d4224 |
|
MD5 | 76611fee5fb8d4a947218c646b794da3 |
|
BLAKE2b-256 | 7248fa117763bf274ce675fea1a49ccea82d2bd0176f400cecf721ad6920e3de |
File details
Details for the file flask_gatekeeper-1.0.0-py3-none-any.whl
.
File metadata
- Download URL: flask_gatekeeper-1.0.0-py3-none-any.whl
- Upload date:
- Size: 8.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/4.0.0 CPython/3.10.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | d781fc6ee26eb4d4385c2e8c64ddfe196675f8364211feb3f09d84e30e92d3be |
|
MD5 | 913233478cb2262f1d5831cef09c1f02 |
|
BLAKE2b-256 | 1e2069589c8420509877e2bdbc660169ab6f62093b1b31968a2a1d513dd47020 |