Search code examples
pythonflaskrate-limiting

Flask Limiter error: TypeError: Limiter.__init__() got multiple values for argument 'key_func'


I am attempting to use Flask's rate limiting library to rate limit an API based on the seconds.

So I have used this exact same format to limit requests to an API on an Apache Server. However I am now using an NGINX. I do not thinks this makes a difference but when I run this code:

import api

app = Flask(__name__, instance_relative_config=True)

limiter = Limiter(app, default_limits=["5/second"], key_func=lambda: get_remote_address)

limiter.limit("5/second", key_func=lambda: request.args.get('token') if 'token' in request.args else get_remote_address)(api.bp)

app.register_blueprint(api.bp)

Again I have ran this exact same code on another server, but now it is giving this error:

limiter = Limiter(app, "5/second", key_func=lambda:
request.args.get('token') if 'token' in request.args else get_remote_address)

TypeError: Limiter.__init__() got multiple values for argument 'key_func'

Any help would be great. I am using Flask-Limiter in python and running gevent on gunicorn server for NGINX.


Solution

  • Your Limiter class instantiation is incorrect. Below is the correct one-

    limiter = Limiter(get_remote_address, app=app, default_limits=["200 per day", "50 per hour"])