Skip to main content

Rate-limiting for django

Project description

django-ratelimiter

TBD

Synopsis

from django.http import HttpRequest, HttpResponse
from django_ratelimiter import ratelimit
from limits.storage import RedisStorage

# defaults, limit all requests
@ratelimit("5/minute")
def view(request: HttpRequest) -> HttpResponse:
    return HttpResponse("OK")

# limit per-user (request attribute key)
@ratelimit("5/minute", key="user")
def view(request: HttpRequest) -> HttpResponse:
    return HttpResponse("OK")

# custom key function
@ratelimit("5/minute", key=lambda r: r.user.username)
def view(request: HttpRequest) -> HttpResponse:
    return HttpResponse("OK")

# limit only specific methods
@ratelimit("5/minute", methods=["POST", "PUT"])
def view(request: HttpRequest) -> HttpResponse:
    return HttpResponse("OK")

# custom response
@ratelimit("5/minute", response=HttpResponse("Too many requests", status=400))
def view(request: HttpRequest) -> HttpResponse:
    return HttpResponse("OK")

# Using `limits.storage` storages
@ratelimit("5/minute", storage=RedisStorage(uri="redis://localhost:6379/0"))
def view(request: HttpRequest) -> HttpResponse:
    return HttpResponse("OK")

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_ratelimiter-0.1.0.tar.gz (3.6 kB view details)

Uploaded Source

Built Distribution

django_ratelimiter-0.1.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file django_ratelimiter-0.1.0.tar.gz.

File metadata

  • Download URL: django_ratelimiter-0.1.0.tar.gz
  • Upload date:
  • Size: 3.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.8.2 CPython/3.10.13 Darwin/23.4.0

File hashes

Hashes for django_ratelimiter-0.1.0.tar.gz
Algorithm Hash digest
SHA256 04b9a2f4ac07ec60fdeffccc8409d8de07f8bc159039fa7d1f3f159c6cf45ffe
MD5 991814d000eec5e857167fbd1644eafd
BLAKE2b-256 e1a443e34e0d9dcf0fb29c43aceeabfd484557d71275ac4bf683aad1ffdaec37

See more details on using hashes here.

File details

Details for the file django_ratelimiter-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for django_ratelimiter-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 56b17830d9ab7969228e8d59b3c24dbfde64b0542b870cb875146e8d2dcd5b03
MD5 dd60b2831813f798687f9e1d8f7c542e
BLAKE2b-256 9c0019be1d2864f79fefadbb412d17c0d4c56008c12782105d93c4fffdbf9ec3

See more details on using hashes here.

Supported by

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