A (very) simple banning & rate limiting extension for Flask.
Project description
flask-gatekeeper
A (very) 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's simple, does not require any dependancies, and quite fast due to the use of collections.deque
and minimal storage of information regarding the clients.
Install
pip install flask-gatekeeper
Usage
Here is a demo app showing all the capabilities of flask-gatekeeper :
from flask import Flask
from flask_gatekeeper import GateKeeper # important
# create our flask app
app = Flask(__name__)
# add our GateKeeper instance with global rules
gk = GateKeeper(app,ban_rule=[3,60,600],rate_limit_rule=[100,60])
@app.route("/ping")
def ping():
return "ok",200
@app.route("/login")
def login():
if password_is_ok():
return token,200
else:
gk.report() # ban if an IP is "reported" 3 times in less than 60s
return "bad password",401
@app.route("/specific")
@gk.specific(rate_limit_rule=[1,10]) # add another rate limit on top of the global one
def specific():
return "ok",200
@app.route("/specific")
@gk.specific(rate_limit_rule=[1,10],standalone=True) # route only limited by the specific rule
def specific():
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",5001)
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
flask_gatekeeper-0.2.tar.gz
(5.1 kB
view hashes)
Built Distribution
Close
Hashes for flask_gatekeeper-0.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | e54a494c796193443d203c844457e2811ce7badb42cb27e0d7cfa51229a126df |
|
MD5 | ff328407a32efb1e4127cb6c1db4c868 |
|
BLAKE2b-256 | c0b0c8b5d6893c85149592d514454af7ada8d3a1fa57e18e0efd1c51431503f8 |