Skip to main content

Flask_Limit

An extension that provides rate limiting for Flask routes.

Installation

The easiest way to install this is through pip.

pip install Flask_Limit

Configuration

This extension depends on two configuration parameters RATELIMITE_LIMIT and RATELIMIT_PERIOD. If this parameters are not set, default values of 10 and 20 are used respectively, which represents the number of allowed requests(limit) within a given time(period).

Basic Usage

The easiest way to rate limit the entire application is limit the application's before request method. The rate_limit decorator can be called with or without the litmit and period paramters. If this parameters are not provided, the values are gotten from the application's configuration. In the example below, after rate limiting the before_request method, a get request to /greet/ will show from the response headers that the rate limiting is working.

from flask import Flask, g
from flask_limit import RateLimiter

class Config:
	RATELIMITE_LIMIT = 10
	RATELIMIT_PERIOD = 30

app = Flask(__name__)
app.config.from_object(Config)
limiter = RateLimiter(app)

@app.before_request
@limiter.rate_limit
def before_request():
    pass

@app.after_request
def after_request(rv):
    headers = getattr(g, 'headers', {})
    rv.headers.extend(headers)
    return rv


@app.route('/greet/<name>')
def greet(name):
    return f'Hello {name}!'


if __name__ == '__main__':
    app.run()

Complex example

More than one route can be rate limited.

from flask import Flask, g
from flask_limit import RateLimiter

class Config:
	RATELIMITE_LIMIT = 10
	RATELIMIT_PERIOD = 30

app = Flask(__name__)
app.config.from_object(Config)
limiter = RateLimiter(app)

@app.before_request
@limiter.rate_limit
def before_request():
    pass

@app.after_request
def after_request(rv):
    headers = getattr(g, 'headers', {})
    rv.headers.extend(headers)
    return rv


@app.route('/greet/<name>')
def greet(name):
    return f'Hello {name}!'


@app.route('/get-auth-token')
@limiter.rate_limit(limit=1, period=600)  # one call per 10 minute period
def get_auth_token():
    return {'token': '<auth-token>'}

if __name__ == '__main__':
    app.run()

Proof

proof

Credit

Credit to Miguel Grinberg for his exception work on rate limiting, from which this extension is based on.

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Flask_Limit-1.0.3.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

Flask_Limit-1.0.3-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file Flask_Limit-1.0.3.tar.gz.

File metadata

  • Download URL: Flask_Limit-1.0.3.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.8

File hashes

Hashes for Flask_Limit-1.0.3.tar.gz
Algorithm Hash digest
SHA256 298679661cdb0130702a5dfa6ada50dd52d93827b6aa03e90044d72f8dcfa1f0
MD5 2a716ebb8ee492326f12a5fc859c4033
BLAKE2b-256 335fe4b857a55af90a1023ad6d9d850286e24f7cdcbe71cc6fc604f70969c20e

See more details on using hashes here.

File details

Details for the file Flask_Limit-1.0.3-py3-none-any.whl.

File metadata

  • Download URL: Flask_Limit-1.0.3-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/4.0.2 CPython/3.10.8

File hashes

Hashes for Flask_Limit-1.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 ccbd6a297917d095caf0ae3f7f3c495b4c17bf480341ed255e09c7143190fb6d
MD5 a8da7f63d60de5d32504d0a0408a77d6
BLAKE2b-256 974f8c9d77554cb5642aee039552ec4efc0c5e7534a99b5b584c5a6cefc336db

See more details on using hashes here.

Release history Release notifications | RSS feed

3.0.0

2 files

2.0.5

1 file

2.0.4

1 file

2.0.3

1 file

2.0.2

1 file

2.0.1

1 file

2.0

1 file

1.0.5

1 file

1.0.4

1 file

This release

1.0.3 This release

2 files

1.0.2

2 files

1.0.1

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page