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 hashes)

Uploaded Source

Built Distribution

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

Uploaded Python 3

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