METADATA 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. Metadata-Version: 2.2
  2. Name: Flask-Limiter
  3. Version: 3.12
  4. Summary: Rate limiting for flask applications
  5. Home-page: https://flask-limiter.readthedocs.org
  6. Author: Ali-Akber Saifee
  7. Author-email: ali@indydevs.org
  8. License: MIT
  9. Project-URL: Source, https://github.com/alisaifee/flask-limiter
  10. Classifier: Development Status :: 5 - Production/Stable
  11. Classifier: Environment :: Web Environment
  12. Classifier: Framework :: Flask
  13. Classifier: Intended Audience :: Developers
  14. Classifier: License :: OSI Approved :: MIT License
  15. Classifier: Operating System :: MacOS
  16. Classifier: Operating System :: POSIX :: Linux
  17. Classifier: Operating System :: OS Independent
  18. Classifier: Topic :: Software Development :: Libraries :: Python Modules
  19. Classifier: Programming Language :: Python :: 3.10
  20. Classifier: Programming Language :: Python :: 3.11
  21. Classifier: Programming Language :: Python :: 3.12
  22. Classifier: Programming Language :: Python :: 3.13
  23. Requires-Python: >=3.10
  24. License-File: LICENSE.txt
  25. Requires-Dist: limits>=3.13
  26. Requires-Dist: Flask>=2
  27. Requires-Dist: ordered-set<5,>4
  28. Requires-Dist: rich<14,>=12
  29. Provides-Extra: redis
  30. Requires-Dist: limits[redis]; extra == "redis"
  31. Provides-Extra: memcached
  32. Requires-Dist: limits[memcached]; extra == "memcached"
  33. Provides-Extra: mongodb
  34. Requires-Dist: limits[mongodb]; extra == "mongodb"
  35. Provides-Extra: valkey
  36. Requires-Dist: limits[valkey]; extra == "valkey"
  37. Dynamic: author
  38. Dynamic: author-email
  39. Dynamic: classifier
  40. Dynamic: description
  41. Dynamic: home-page
  42. Dynamic: license
  43. Dynamic: project-url
  44. Dynamic: provides-extra
  45. Dynamic: requires-dist
  46. Dynamic: requires-python
  47. Dynamic: summary
  48. .. |ci| image:: https://github.com/alisaifee/flask-limiter/actions/workflows/main.yml/badge.svg?branch=master
  49. :target: https://github.com/alisaifee/flask-limiter/actions?query=branch%3Amaster+workflow%3ACI
  50. .. |codecov| image:: https://codecov.io/gh/alisaifee/flask-limiter/branch/master/graph/badge.svg
  51. :target: https://codecov.io/gh/alisaifee/flask-limiter
  52. .. |pypi| image:: https://img.shields.io/pypi/v/Flask-Limiter.svg?style=flat-square
  53. :target: https://pypi.python.org/pypi/Flask-Limiter
  54. .. |license| image:: https://img.shields.io/pypi/l/Flask-Limiter.svg?style=flat-square
  55. :target: https://pypi.python.org/pypi/Flask-Limiter
  56. .. |docs| image:: https://readthedocs.org/projects/flask-limiter/badge/?version=latest
  57. :target: https://flask-limiter.readthedocs.org/en/latest
  58. *************
  59. Flask-Limiter
  60. *************
  61. |docs| |ci| |codecov| |pypi| |license|
  62. **Flask-Limiter** adds rate limiting to `Flask <https://flask.palletsprojects.com>`_ applications.
  63. You can configure rate limits at different levels such as:
  64. - Application wide global limits per user
  65. - Default limits per route
  66. - By `Blueprints <https://flask-limiter.readthedocs.io/en/latest/recipes.html#rate-limiting-all-routes-in-a-blueprint>`_
  67. - By `Class-based views <https://flask-limiter.readthedocs.io/en/latest/recipes.html#using-flask-pluggable-views>`_
  68. - By `individual routes <https://flask-limiter.readthedocs.io/en/latest/index.html#decorators-to-declare-rate-limits>`_
  69. **Flask-Limiter** can be `configured <https://flask-limiter.readthedocs.io/en/latest/configuration.html>`_ to fit your application in many ways, including:
  70. - Persistance to various commonly used `storage backends <https://flask-limiter.readthedocs.io/en/latest/#configuring-a-storage-backend>`_
  71. (such as Redis, Memcached & MongoDB)
  72. via `limits <https://limits.readthedocs.io/en/stable/storage.html>`__
  73. - Any rate limiting strategy supported by `limits <https://limits.readthedocs.io/en/stable/strategies.html>`__
  74. Follow the quickstart below to get started or `read the documentation <http://flask-limiter.readthedocs.org/en/latest>`_ for more details.
  75. Quickstart
  76. ===========
  77. Install
  78. -------
  79. .. code-block:: bash
  80. pip install Flask-Limiter
  81. Add the rate limiter to your flask app
  82. ---------------------------------------
  83. .. code-block:: python
  84. # app.py
  85. from flask import Flask
  86. from flask_limiter import Limiter
  87. from flask_limiter.util import get_remote_address
  88. app = Flask(__name__)
  89. limiter = Limiter(
  90. get_remote_address,
  91. app=app,
  92. default_limits=["2 per minute", "1 per second"],
  93. storage_uri="memory://",
  94. # Redis
  95. # storage_uri="redis://localhost:6379",
  96. # Redis cluster
  97. # storage_uri="redis+cluster://localhost:7000,localhost:7001,localhost:70002",
  98. # Memcached
  99. # storage_uri="memcached://localhost:11211",
  100. # Memcached Cluster
  101. # storage_uri="memcached://localhost:11211,localhost:11212,localhost:11213",
  102. # MongoDB
  103. # storage_uri="mongodb://localhost:27017",
  104. strategy="fixed-window", # or "moving-window", or "sliding-window-counter"
  105. )
  106. @app.route("/slow")
  107. @limiter.limit("1 per day")
  108. def slow():
  109. return "24"
  110. @app.route("/fast")
  111. def fast():
  112. return "42"
  113. @app.route("/ping")
  114. @limiter.exempt
  115. def ping():
  116. return 'PONG'
  117. Inspect the limits using the command line interface
  118. ---------------------------------------------------
  119. .. code-block:: bash
  120. $ FLASK_APP=app:app flask limiter limits
  121. app
  122. ├── fast: /fast
  123. │ ├── 2 per 1 minute
  124. │ └── 1 per 1 second
  125. ├── ping: /ping
  126. │ └── Exempt
  127. └── slow: /slow
  128. └── 1 per 1 day
  129. Run the app
  130. -----------
  131. .. code-block:: bash
  132. $ FLASK_APP=app:app flask run
  133. Test it out
  134. -----------
  135. The ``fast`` endpoint respects the default rate limit while the
  136. ``slow`` endpoint uses the decorated one. ``ping`` has no rate limit associated
  137. with it.
  138. .. code-block:: bash
  139. $ curl localhost:5000/fast
  140. 42
  141. $ curl localhost:5000/fast
  142. 42
  143. $ curl localhost:5000/fast
  144. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
  145. <title>429 Too Many Requests</title>
  146. <h1>Too Many Requests</h1>
  147. <p>2 per 1 minute</p>
  148. $ curl localhost:5000/slow
  149. 24
  150. $ curl localhost:5000/slow
  151. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 3.2 Final//EN">
  152. <title>429 Too Many Requests</title>
  153. <h1>Too Many Requests</h1>
  154. <p>1 per 1 day</p>
  155. $ curl localhost:5000/ping
  156. PONG
  157. $ curl localhost:5000/ping
  158. PONG
  159. $ curl localhost:5000/ping
  160. PONG
  161. $ curl localhost:5000/ping
  162. PONG