Flask-Limit


NameFlask-Limit JSON
Version 2.0.5 PyPI version JSON
download
home_pagehttps://github.com/tabotkevin/flask_limit
SummaryAn extension that provides rate limiting for Flask routes.
upload_time2023-08-27 17:32:29
maintainer
docs_urlNone
authorTabot Kevin
requires_python>=3.6
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage No coveralls.
            # Flask_Limit

[![Build status](https://github.com/tabotkevin/flask_limit/actions/workflows/tests.yml/badge.svg)](https://github.com/tabotkevin/flask_limit/actions)

An extension that provides rate limiting for Flask routes.

## Installation

The easiest way to install this is through pip.

```shell
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/<name>**
will show from the response headers that the rate limiting is working.

## In-Memory example

This example uses `MemRateLimit` which is the default Rate-Limiter.

```python
from flask import Flask, g
from flask_limit import RateLimiter

class Config:
	RATELIMIT_LIMIT = 10  # Number of allowed requests.
	RATELIMIT_PERIOD = 30  # Period in seconds.

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()
```

## Redis example

This example uses `RedisRateLimit` as the Rate-Limiter.
Remember to set the `REDIS_URL` parameter in your configuration

```python
from flask import Flask, g
from flask_limit import RateLimiter

class Config:
    RATELIMIT_LIMIT = 10
    RATELIMIT_PERIOD = 30
    REDIS_URL = "redis://localhost:6379/0"


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


@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.

```python
from flask import Flask, g
from flask_limit import RateLimiter

class Config:
	RATELIMIT_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()
```

## Tests

```shell
  python -m pip install --upgrade pip
  pip install tox tox-gh-actions

  tox
```

## Proof

![proof](proof.png)

            

Raw data

            {
    "_id": null,
    "home_page": "https://github.com/tabotkevin/flask_limit",
    "name": "Flask-Limit",
    "maintainer": "",
    "docs_url": null,
    "requires_python": ">=3.6",
    "maintainer_email": "",
    "keywords": "",
    "author": "Tabot Kevin",
    "author_email": "tabot.kevin@gmail.com",
    "download_url": "https://files.pythonhosted.org/packages/15/66/4b6f4559509d8b1b2122461f0edc87e6a0b08b829148b9a1e80758ee10d9/Flask_Limit-2.0.5.tar.gz",
    "platform": null,
    "description": "# Flask_Limit\n\n[![Build status](https://github.com/tabotkevin/flask_limit/actions/workflows/tests.yml/badge.svg)](https://github.com/tabotkevin/flask_limit/actions)\n\nAn extension that provides rate limiting for Flask routes.\n\n## Installation\n\nThe easiest way to install this is through pip.\n\n```shell\npip install Flask_Limit\n```\n\n## Configuration\n\nThis extension depends on two configuration parameters **RATELIMITE_LIMIT** and **RATELIMIT_PERIOD**.\nIf this parameters are not set, default values of **10** and **20** are used respectively,\nwhich represents the number of allowed requests(limit) within a given time(period).\n\n## Basic Usage\n\nThe easiest way to rate limit the entire application is limit the application's before request method.\nThe **rate_limit** decorator can be called with or without the **litmit** and **period** paramters.\nIf this parameters are not provided, the values are gotten from the application's configuration.\nIn the example below, after rate limiting the **before_request** method, a get request to **/greet/<name>**\nwill show from the response headers that the rate limiting is working.\n\n## In-Memory example\n\nThis example uses `MemRateLimit` which is the default Rate-Limiter.\n\n```python\nfrom flask import Flask, g\nfrom flask_limit import RateLimiter\n\nclass Config:\n\tRATELIMIT_LIMIT = 10  # Number of allowed requests.\n\tRATELIMIT_PERIOD = 30  # Period in seconds.\n\napp = Flask(__name__)\napp.config.from_object(Config)\nlimiter = RateLimiter(app)\n\n@app.before_request\n@limiter.rate_limit\ndef before_request():\n    pass\n\n@app.after_request\ndef after_request(rv):\n    headers = getattr(g, 'headers', {})\n    rv.headers.extend(headers)\n    return rv\n\n\n@app.route('/greet/<name>')\ndef greet(name):\n    return f'Hello {name}!'\n\n\nif __name__ == '__main__':\n    app.run()\n```\n\n## Redis example\n\nThis example uses `RedisRateLimit` as the Rate-Limiter.\nRemember to set the `REDIS_URL` parameter in your configuration\n\n```python\nfrom flask import Flask, g\nfrom flask_limit import RateLimiter\n\nclass Config:\n    RATELIMIT_LIMIT = 10\n    RATELIMIT_PERIOD = 30\n    REDIS_URL = \"redis://localhost:6379/0\"\n\n\napp = Flask(__name__)\napp.config.from_object(Config)\nlimiter = RateLimiter(app, limiter=\"redis\")\n\n\n@app.before_request\n@limiter.rate_limit\ndef before_request():\n    pass\n\n\n@app.after_request\ndef after_request(rv):\n    headers = getattr(g, \"headers\", {})\n    rv.headers.extend(headers)\n    return rv\n\n\n@app.route(\"/greet/<name>\")\ndef greet(name):\n    return f\"Hello {name}!\"\n\n\nif __name__ == \"__main__\":\n    app.run()\n```\n\n## Complex example\n\nMore than one route can be rate limited.\n\n```python\nfrom flask import Flask, g\nfrom flask_limit import RateLimiter\n\nclass Config:\n\tRATELIMIT_LIMIT = 10\n\tRATELIMIT_PERIOD = 30\n\napp = Flask(__name__)\napp.config.from_object(Config)\nlimiter = RateLimiter(app)\n\n@app.before_request\n@limiter.rate_limit\ndef before_request():\n    pass\n\n@app.after_request\ndef after_request(rv):\n    headers = getattr(g, 'headers', {})\n    rv.headers.extend(headers)\n    return rv\n\n\n@app.route('/greet/<name>')\ndef greet(name):\n    return f'Hello {name}!'\n\n\n@app.route('/get-auth-token')\n@limiter.rate_limit(limit=1, period=600)  # one call per 10 minute period\ndef get_auth_token():\n    return {'token': '<auth-token>'}\n\nif __name__ == '__main__':\n    app.run()\n```\n\n## Tests\n\n```shell\n  python -m pip install --upgrade pip\n  pip install tox tox-gh-actions\n\n  tox\n```\n\n## Proof\n\n![proof](proof.png)\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "An extension that provides rate limiting for Flask routes.",
    "version": "2.0.5",
    "project_urls": {
        "Bug Tracker": "https://github.com/tabotkevin/flask_limit/issues",
        "Homepage": "https://github.com/tabotkevin/flask_limit"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "15664b6f4559509d8b1b2122461f0edc87e6a0b08b829148b9a1e80758ee10d9",
                "md5": "679f808536153b6d6bc5d4115c6d6f60",
                "sha256": "d885614eac17be904a8be41d8b8020b2dd0d8ffb4390f045214a0a447ee05986"
            },
            "downloads": -1,
            "filename": "Flask_Limit-2.0.5.tar.gz",
            "has_sig": false,
            "md5_digest": "679f808536153b6d6bc5d4115c6d6f60",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.6",
            "size": 698359,
            "upload_time": "2023-08-27T17:32:29",
            "upload_time_iso_8601": "2023-08-27T17:32:29.461495Z",
            "url": "https://files.pythonhosted.org/packages/15/66/4b6f4559509d8b1b2122461f0edc87e6a0b08b829148b9a1e80758ee10d9/Flask_Limit-2.0.5.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2023-08-27 17:32:29",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "tabotkevin",
    "github_project": "flask_limit",
    "travis_ci": false,
    "coveralls": false,
    "github_actions": true,
    "requirements": [],
    "tox": true,
    "lcname": "flask-limit"
}
        
Elapsed time: 0.11995s