1

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?

Nanno
  • 71
  • 1
  • 7

1 Answers1

0

It was possible to fix the problem by setting storage_uri to use "redis", and activating redis in my web hosting account.

This is how to implement Flask-Limiter with redis:

limiter = Limiter(app, key_func=get_remote_address, storage_uri="redis://localhost:6379")

The 6379 is the default port for redis.

More info at: https://flask-limiter.readthedocs.io/en/latest/#configuring-a-storage-backend

Nanno
  • 71
  • 1
  • 7