Flask-Limiter


NameFlask-Limiter JSON
Version 3.9.2 PyPI version JSON
download
home_pagehttps://flask-limiter.readthedocs.org
SummaryRate limiting for flask applications
upload_time2024-11-27 20:19:59
maintainerNone
docs_urlNone
authorAli-Akber Saifee
requires_python>=3.9
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.9",
    "maintainer_email": null,
    "keywords": null,
    "author": "Ali-Akber Saifee",
    "author_email": "ali@indydevs.org",
    "download_url": "https://files.pythonhosted.org/packages/26/e5/196a08f25791c561e1f39c6dfd44c6b8b54e9c0947daa34427b0099e9f0b/flask_limiter-3.9.2.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.9.2",
    "project_urls": {
        "Homepage": "https://flask-limiter.readthedocs.org",
        "Source": "https://github.com/alisaifee/flask-limiter"
    },
    "split_keywords": [],
    "urls": [
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "7866a6c16dd1b071cebe1c7aa45807a91fd5cf00822780ab8d02ccf32fff2611",
                "md5": "d74b4d2dc2900bc88160f0977a711399",
                "sha256": "d1e98c58655703f0a30dbc7c69b6b14afb5a54845cedee3769525327473bfaeb"
            },
            "downloads": -1,
            "filename": "Flask_Limiter-3.9.2-py3-none-any.whl",
            "has_sig": false,
            "md5_digest": "d74b4d2dc2900bc88160f0977a711399",
            "packagetype": "bdist_wheel",
            "python_version": "py3",
            "requires_python": ">=3.9",
            "size": 28632,
            "upload_time": "2024-11-27T20:19:57",
            "upload_time_iso_8601": "2024-11-27T20:19:57.759075Z",
            "url": "https://files.pythonhosted.org/packages/78/66/a6c16dd1b071cebe1c7aa45807a91fd5cf00822780ab8d02ccf32fff2611/Flask_Limiter-3.9.2-py3-none-any.whl",
            "yanked": false,
            "yanked_reason": null
        },
        {
            "comment_text": "",
            "digests": {
                "blake2b_256": "26e5196a08f25791c561e1f39c6dfd44c6b8b54e9c0947daa34427b0099e9f0b",
                "md5": "23a2472ad188cdc89ca6717669589d88",
                "sha256": "b110049cf7f7b1c5e24f01c3c76e34357303ee64d27a9b32e88e84ea8a0fc97b"
            },
            "downloads": -1,
            "filename": "flask_limiter-3.9.2.tar.gz",
            "has_sig": false,
            "md5_digest": "23a2472ad188cdc89ca6717669589d88",
            "packagetype": "sdist",
            "python_version": "source",
            "requires_python": ">=3.9",
            "size": 301929,
            "upload_time": "2024-11-27T20:19:59",
            "upload_time_iso_8601": "2024-11-27T20:19:59.872890Z",
            "url": "https://files.pythonhosted.org/packages/26/e5/196a08f25791c561e1f39c6dfd44c6b8b54e9c0947daa34427b0099e9f0b/flask_limiter-3.9.2.tar.gz",
            "yanked": false,
            "yanked_reason": null
        }
    ],
    "upload_time": "2024-11-27 20:19:59",
    "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: 1.08401s