I developed a web application with Python and Flask.
I have to limit the rate of access based on visitor's IPs, that is, how many times the same IP can access the same webpage in a given time, and for that I am using flask-limiter.
Here is my full 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, default_limits=['300/day'], enabled=True)
counter = 0
@app.route('/')
@limiter.limit('200/day')
@limiter.limit('50/hour')
@limiter.limit('10/minute')
def hello_world():
global counter
counter = counter + 1
return f'Hello World! Visit number: {counter}'
if __name__ == '__main__':
app.run()
It is not working properly on the server (Cloudlinux + Litespeed); it ends-up generating a "time out" error frequently, but not always (a kind of intermittent error).
If I disable flask-limiter by setting enabled=False
, then everything works fine.
What wrong I am doing? Any alternative?