Skip to main content

A simple Django middleware for rate limiting by IP or user

Project description

django-ratelimit-middleware

codecov Python - >=3.12 Django - >=5.2 Style - Black

🌟 Overview

Django Rate Limit Middleware is a simple, lightweight Django middleware for rate limiting requests based on user identity or IP address.
It helps prevent abuse, brute force attacks, and excessive traffic with minimal configuration.

✨ Features

  • 🔑 Supports anonymous and authenticated users
  • ⏱ Configurable request count and time window
  • 🧠 Uses Django’s cache framework
  • 🪶 Lightweight, dependency-free

📦 Installation

pip install django-ratelimit-middleware

⚙️ Configuration

Add to your MIDDLEWARE:

MIDDLEWARE = [
    ...
    "django_ratelimit_middleware.middleware.RateLimitMiddleware",
]

Add settings:

RATE_LIMIT_REQUESTS = 100       # Number of requests allowed
RATE_LIMIT_WINDOW = 60          # Window in seconds

🚀 Usage

After installation and config, all views are automatically rate-limited.
Example response when exceeding the limit:

HTTP/1.1 429 Too Many Requests
Retry-After: 60

🧪 Running Tests

pytest

📊 Coverage

Coverage reports are uploaded to Codecov

🤝 Contributing

Contributions are welcome! 🎉

See CONTRIBUTING.md for details.

📌 Roadmap

  • Custom rate limit strategies (e.g., per endpoint)
  • Redis backend example
  • Admin dashboard for monitoring blocked IPs
  • Configurable responses (JSON, HTML)
  • Turnstile feature

📜 License

This project is licensed under the MIT License - see the LICENSE file for details.

Disclaimer

This software is provided "as is", without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement.
In no event shall the authors or copyright holders be liable for any claim, damages, or other liability, whether in an action of contract, tort, or otherwise, arising from, out of, or in connection with the software or the use or other dealings in the software.
Use at your own risk.

👨‍💻 Author

Maintained with ❤️ by K3y5tr0k3.

Project details


Download files

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

Source Distribution

django_ratelimit_middleware-0.1.1.tar.gz (8.7 kB view details)

Uploaded Source

Built Distribution

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

django_ratelimit_middleware-0.1.1-py3-none-any.whl (5.3 kB view details)

Uploaded Python 3

File details

Details for the file django_ratelimit_middleware-0.1.1.tar.gz.

File metadata

File hashes

Hashes for django_ratelimit_middleware-0.1.1.tar.gz
Algorithm Hash digest
SHA256 eb0b35002864469306feca371cf52248ca31f79d8b97fa7280ded4865834c380
MD5 5e35d1122518ef2032772e43d8c7cd32
BLAKE2b-256 0a644f32623d481b64cc2740667335db6f09284fe05b04739a2d049287aacf2a

See more details on using hashes here.

File details

Details for the file django_ratelimit_middleware-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for django_ratelimit_middleware-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cd295193bb7de7eb590b1ac0b0fb7f57c8e8029baab0945797b0aded4e71f26e
MD5 62d1fa548524851f40ca956081d9a3b2
BLAKE2b-256 e7944056620c9d324300da561c5746f2d5d082f548dc6407b949463f0c910ad7

See more details on using hashes here.

Supported by

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