Reputation: 131
Using Flask, is it possible to make python module that listens requests coming to server by users and count requests. After some time if any of users make, for example, 300 000 requests I want to restrict access to service they are using.
Regarding services. Our costumers use standard OGC (WMS, WFS) services to get access to our spatial data. They access data via URL we provide them when we make service. One service runs on one port. We have multiple services running on multiple ports on one server.
When get request in sent from client to server I want to request first come to flask app and check for number of requests that user made. If number of requests is less than specified number the app should pass request on but if number of request is more than specified number the app should restrict access to data for that user.
I am willing to hear about other technologies that would make this idea work, but python is preferable.
Upvotes: 2
Views: 1812
Reputation: 506
In flask there is an extension called flask limiter. This extension allows you to limit the amount of request on a particular route. Each route can have different request limits or be exempt to have no limits. It limits user by keeping track of their IP address.
See this documentation: https://flask-limiter.readthedocs.io/en/stable/
example code:
from flask import Flask
from flask_limiter import Limiter
from flask_limiter.util import get_remote_address
app = Flask(__name__)
limiter = Limiter(
app,
key_func=get_remote_address, # get users IP address.
default_limits=["300000 per month"], # default limit to 300K request per month
)
@app.route("/spatial-data")
@limiter.limit("300000 per month")
def slow():
return "720"
app.run()
Upvotes: 1