Flask-Limiter


NameFlask-Limiter JSON
Version 3.7.0 PyPI version JSON
download
home_pagehttps://flask-limiter.readthedocs.org
SummaryRate limiting for flask applications
upload_time2024-05-19 15:56:11
maintainerNone
docs_urlNone
authorAli-Akber Saifee
requires_python>=3.8
licenseMIT
keywords
VCS
bugtrack_url
requirements No requirements were recorded.
Travis-CI No Travis.
coveralls test coverage
            .. |ci| image:: https://github.com/alisaifee/flask-limiter/workflows/CI/badge.svg?branch=master
   :target: https://github.com/alisaifee/flask-limiter/actions?query=branch%3Amaster+workflow%3ACI
.. |codecov| image:: https://codecov.io/gh/alisaifee/flask-limiter/branch/master/graph/badge.svg
   :target: https://codecov.io/gh/alisaifee/flask-limiter
.. |pypi| image:: https://img.shields.io/pypi/v/Flask-Limiter.svg?style=flat-square
   :target: https://pypi.python.org/pypi/Flask-Limiter
.. |license| image:: https://img.shields.io/pypi/l/Flask-Limiter.svg?style=flat-square
   :target: https://pypi.python.org/pypi/Flask-Limiter
.. |docs| image:: https://readthedocs.org/projects/flask-limiter/badge/?version=latest
   :target: https://flask-limiter.readthedocs.org/en/latest

*************
Flask-Limiter
*************


|docs| |ci| |codecov| |pypi| |license|

**Flask-Limiter** adds rate limiting to `Flask <https://flask.palletsprojects.com>`_ applications.

You can configure rate limits at different levels such as:

- Application wide global limits per user
- Default limits per route
- By `Blueprints <https://flask-limiter.readthedocs.io/en/latest/recipes.html#rate-limiting-all-routes-in-a-blueprint>`_
- By `Class-based views <https://flask-limiter.readthedocs.io/en/latest/recipes.html#using-flask-pluggable-views>`_
- By `individual routes <https://flask-limiter.readthedocs.io/en/latest/index.html#decorators-to-declare-rate-limits>`_

**Flask-Limiter** can be `configured <https://flask-limiter.readthedocs.io/en/latest/configuration.html>`_ to fit your application in many ways, including:

- Persistance to various commonly used `storage backends <https://flask-limiter.readthedocs.io/en/latest/#configuring-a-storage-backend>`_
  (such as Redis, Memcached, MongoDB & Etcd)
  via `limits <https://limits.readthedocs.io/en/stable/storage.html>`__
- Any rate limiting strategy supported by `limits <https://limits.readthedocs.io/en/stable/strategies.html>`__

Follow the quickstart below to get started or `read the documentation <http://flask-limiter.readthedocs.org/en/latest>`_ for more details.


Quickstart
===========

Install
-------
.. code-block:: bash

    pip install Flask-Limiter

Add the rate limiter to your flask app
---------------------------------------
.. code-block:: python

   # app.py

   from flask import Flask
   from flask_limiter import Limiter
   from flask_limiter.util import get_remote_address

   app = Flask(__name__)
   limiter = Limiter(
       get_remote_address,
       app=app,
       default_limits=["2 per minute", "1 per second"],
       storage_uri="memory://",
       # Redis
       # storage_uri="redis://localhost:6379",
       # Redis cluster
       # storage_uri="redis+cluster://localhost:7000,localhost:7001,localhost:70002",
       # Memcached
       # storage_uri="memcached://localhost:11211",
       # Memcached Cluster
       # storage_uri="memcached://localhost:11211,localhost:11212,localhost:11213",
       # MongoDB
       # storage_uri="mongodb://localhost:27017",
       # Etcd
       # storage_uri="etcd://localhost:2379",
       strategy="fixed-window", # or "moving-window"
   )

   @app.route("/slow")
   @limiter.limit("1 per day")
   def slow():
       return "24"

   @app.route("/fast")
   def fast():
       return "42"

   @app.route("/ping")
   @limiter.exempt
   def ping():
       return 'PONG'

Inspect the limits using the command line interface
---------------------------------------------------
.. code-block:: bash

   $ FLASK_APP=app:app flask limiter limits

   app
   ├── fast: /fast
   │   ├── 2 per 1 minute
   │   └── 1 per 1 second
   ├── ping: /ping
   │   └── Exempt
   └── slow: /slow
       └── 1 per 1 day

Run the app
-----------
.. code-block:: bash

   $ FLASK_APP=app:app flask run


Test it out
-----------
The ``fast`` endpoint respects the default rate limit while the
``slow`` endpoint uses the decorated one. ``ping`` has no rate limit associated
with it.

.. code-block:: bash

   $ curl localhost:5000/fast
   42
   $ curl localhost:5000/fast
   42
   $ curl localhost:5000/fast
   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
   <title>429 Too Many Requests</title>
   <h1>Too Many Requests</h1>
   <p>2 per 1 minute</p>
   $ curl localhost:5000/slow
   24
   $ curl localhost:5000/slow
   <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
   <title>429 Too Many Requests</title>
   <h1>Too Many Requests</h1>
   <p>1 per 1 day</p>
   $ curl localhost:5000/ping
   PONG
   $ curl localhost:5000/ping
   PONG
   $ curl localhost:5000/ping
   PONG
   $ curl localhost:5000/ping
   PONG





            

Raw data

            {
    "_id": null,
    "home_page": "https://flask-limiter.readthedocs.org",
    "name": "Flask-Limiter",
    "maintainer": null,
    "docs_url": null,
    "requires_python": ">=3.8",
    "maintainer_email": null,
    "keywords": null,
    "author": "Ali-Akber Saifee",
    "author_email": "ali@indydevs.org",
    "download_url": "https://files.pythonhosted.org/packages/ee/81/271b526492252d1befe67f1f8239a21d192e002decbb27396147a478fd4c/flask_limiter-3.7.0.tar.gz",
    "platform": null,
    "description": ".. |ci| image:: https://github.com/alisaifee/flask-limiter/workflows/CI/badge.svg?branch=master\n   :target: https://github.com/alisaifee/flask-limiter/actions?query=branch%3Amaster+workflow%3ACI\n.. |codecov| image:: https://codecov.io/gh/alisaifee/flask-limiter/branch/master/graph/badge.svg\n   :target: https://codecov.io/gh/alisaifee/flask-limiter\n.. |pypi| image:: https://img.shields.io/pypi/v/Flask-Limiter.svg?style=flat-square\n   :target: https://pypi.python.org/pypi/Flask-Limiter\n.. |license| image:: https://img.shields.io/pypi/l/Flask-Limiter.svg?style=flat-square\n   :target: https://pypi.python.org/pypi/Flask-Limiter\n.. |docs| image:: https://readthedocs.org/projects/flask-limiter/badge/?version=latest\n   :target: https://flask-limiter.readthedocs.org/en/latest\n\n*************\nFlask-Limiter\n*************\n\n\n|docs| |ci| |codecov| |pypi| |license|\n\n**Flask-Limiter** adds rate limiting to `Flask <https://flask.palletsprojects.com>`_ applications.\n\nYou can configure rate limits at different levels such as:\n\n- Application wide global limits per user\n- Default limits per route\n- By `Blueprints <https://flask-limiter.readthedocs.io/en/latest/recipes.html#rate-limiting-all-routes-in-a-blueprint>`_\n- By `Class-based views <https://flask-limiter.readthedocs.io/en/latest/recipes.html#using-flask-pluggable-views>`_\n- By `individual routes <https://flask-limiter.readthedocs.io/en/latest/index.html#decorators-to-declare-rate-limits>`_\n\n**Flask-Limiter** can be `configured <https://flask-limiter.readthedocs.io/en/latest/configuration.html>`_ to fit your application in many ways, including:\n\n- Persistance to various commonly used `storage backends <https://flask-limiter.readthedocs.io/en/latest/#configuring-a-storage-backend>`_\n  (such as Redis, Memcached, MongoDB & Etcd)\n  via `limits <https://limits.readthedocs.io/en/stable/storage.html>`__\n- Any rate limiting strategy supported by `limits <https://limits.readthedocs.io/en/stable/strategies.html>`__\n\nFollow the quickstart below to get started or `read the documentation <http://flask-limiter.readthedocs.org/en/latest>`_ for more details.\n\n\nQuickstart\n===========\n\nInstall\n-------\n.. code-block:: bash\n\n    pip install Flask-Limiter\n\nAdd the rate limiter to your flask app\n---------------------------------------\n.. code-block:: python\n\n   # app.py\n\n   from flask import Flask\n   from flask_limiter import Limiter\n   from flask_limiter.util import get_remote_address\n\n   app = Flask(__name__)\n   limiter = Limiter(\n       get_remote_address,\n       app=app,\n       default_limits=[\"2 per minute\", \"1 per second\"],\n       storage_uri=\"memory://\",\n       # Redis\n       # storage_uri=\"redis://localhost:6379\",\n       # Redis cluster\n       # storage_uri=\"redis+cluster://localhost:7000,localhost:7001,localhost:70002\",\n       # Memcached\n       # storage_uri=\"memcached://localhost:11211\",\n       # Memcached Cluster\n       # storage_uri=\"memcached://localhost:11211,localhost:11212,localhost:11213\",\n       # MongoDB\n       # storage_uri=\"mongodb://localhost:27017\",\n       # Etcd\n       # storage_uri=\"etcd://localhost:2379\",\n       strategy=\"fixed-window\", # or \"moving-window\"\n   )\n\n   @app.route(\"/slow\")\n   @limiter.limit(\"1 per day\")\n   def slow():\n       return \"24\"\n\n   @app.route(\"/fast\")\n   def fast():\n       return \"42\"\n\n   @app.route(\"/ping\")\n   @limiter.exempt\n   def ping():\n       return 'PONG'\n\nInspect the limits using the command line interface\n---------------------------------------------------\n.. code-block:: bash\n\n   $ FLASK_APP=app:app flask limiter limits\n\n   app\n   \u251c\u2500\u2500 fast: /fast\n   \u2502   \u251c\u2500\u2500 2 per 1 minute\n   \u2502   \u2514\u2500\u2500 1 per 1 second\n   \u251c\u2500\u2500 ping: /ping\n   \u2502   \u2514\u2500\u2500 Exempt\n   \u2514\u2500\u2500 slow: /slow\n       \u2514\u2500\u2500 1 per 1 day\n\nRun the app\n-----------\n.. code-block:: bash\n\n   $ FLASK_APP=app:app flask run\n\n\nTest it out\n-----------\nThe ``fast`` endpoint respects the default rate limit while the\n``slow`` endpoint uses the decorated one. ``ping`` has no rate limit associated\nwith it.\n\n.. code-block:: bash\n\n   $ curl localhost:5000/fast\n   42\n   $ curl localhost:5000/fast\n   42\n   $ curl localhost:5000/fast\n   <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n   <title>429 Too Many Requests</title>\n   <h1>Too Many Requests</h1>\n   <p>2 per 1 minute</p>\n   $ curl localhost:5000/slow\n   24\n   $ curl localhost:5000/slow\n   <!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 3.2 Final//EN\">\n   <title>429 Too Many Requests</title>\n   <h1>Too Many Requests</h1>\n   <p>1 per 1 day</p>\n   $ curl localhost:5000/ping\n   PONG\n   $ curl localhost:5000/ping\n   PONG\n   $ curl localhost:5000/ping\n   PONG\n   $ curl localhost:5000/ping\n   PONG\n\n\n\n\n",
    "bugtrack_url": null,
    "license": "MIT",
    "summary": "Rate limiting for flask applications",
    "version": "3.7.0",
    "project_urls": {
        "Homepage": "https://flask-limiter.readthedocs.org",
        "Source": "https://github.com/alisaifee/flask-limiter"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "09d3f10e91728a0d33cce23e46eb05948dc38e959018cddf15103af0e08eeec6",
                "md5": "6ebc999684b09c423c5557a01bee9d92",
                "sha256": "4318382f17ecb09848bc6d0f7bc4bb1bf89bcf162200bf47b7b969126693bfda"
            },
            "downloads": -1,
            "filename": "Flask_Limiter-3.7.0-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "6ebc999684b09c423c5557a01bee9d92",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.8",
            "size": 28636,
            "upload_time": "2024-05-19T15:56:09",
            "upload_time_iso_8601": "2024-05-19T15:56:09.599506Z",
            "url": "https://files.pythonhosted.org/packages/09/d3/f10e91728a0d33cce23e46eb05948dc38e959018cddf15103af0e08eeec6/Flask_Limiter-3.7.0-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "ee81271b526492252d1befe67f1f8239a21d192e002decbb27396147a478fd4c",
                "md5": "a55d7b22f40ee4e6644721ab6c994a96",
                "sha256": "e474462505f6dd0d776db16c46092e9a065ebcb30b10aed0caf54c6b9a4a471a"
            },
            "downloads": -1,
            "filename": "flask_limiter-3.7.0.tar.gz",
            "has_sig": false,
            "md5_digest": "a55d7b22f40ee4e6644721ab6c994a96",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.8",
            "size": 301800,
            "upload_time": "2024-05-19T15:56:11",
            "upload_time_iso_8601": "2024-05-19T15:56:11.528547Z",
            "url": "https://files.pythonhosted.org/packages/ee/81/271b526492252d1befe67f1f8239a21d192e002decbb27396147a478fd4c/flask_limiter-3.7.0.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-05-19 15:56:11",
    "github": true,
    "gitlab": false,
    "bitbucket": false,
    "codeberg": false,
    "github_user": "alisaifee",
    "github_project": "flask-limiter",
    "travis_ci": false,
    "coveralls": true,
    "github_actions": true,
    "lcname": "flask-limiter"
}
        
Elapsed time: 0.26691s