A Django framework for application-layer rate limiting
Project description
a framework for implementing application-specific rate-limiting middleware for Django projects
What this module is intended for:
Implementing application-level (or just below) rate-limiting rules. Often, these rules would be expressed as “max # requests within a defined time period”. E.g.:
an IP address may make at most 1500 requests/day
users with an OAuth access token may make 500 reads/hour and 200 writes/hour
What it is not intended for:
A token bucket or leaky bucket filter: intended primarily for traffic shaping, those algorithms are implemented by firewalls and servers such as nginx.
Installation
Install the library with pip:
sudo pip install django-throttle-requests
Add the directory throttle to your project’s PYTHONPATH.
Insert the following configuration into your project’s settings:
THROTTLE_ZONES = { 'default': { 'VARY':'throttle.zones.RemoteIP', 'NUM_BUCKETS':2, # Number of buckets worth of history to keep. Must be at least 2 'BUCKET_INTERVAL':15 * 60, # Period of time to enforce limits. 'BUCKET_CAPACITY':50, # Maximum number of requests allowed within BUCKET_INTERVAL }, } # Where to store request counts. THROTTLE_BACKEND = 'throttle.backends.cache.CacheBackend' # Optional after Redis backend is chosen ('throttle.backends.redispy.RedisBackend') THROTTLE_REDIS_HOST = 'localhost' THROTTLE_REDIS_PORT = 6379 THROTTLE_REDIS_DB = 0 # Force throttling when DEBUG=True THROTTLE_ENABLED = True
Use the @throttle decorator to enforce throttling rules on a view:
from throttle.decorators import throttle @throttle(zone='default') def myview(request): ...
Also works with class-based views:
from django.views.generic import View from django.utils.decorators import method_decorator from throttle.decorators import throttle class TestView(View): @method_decorator(throttle(zone='default')) def dispatch(self, *args, **kwargs): return super(TestView, self).dispatch(*args, **kwargs) def head(self, request): ... def get(self, request): ...
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for django-throttle-requests-0.5.7.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | cb6fffefadbe8d9bb5e60d018313ec4373b3867af70dec9fb76427519d7a7223 |
|
MD5 | dc13c6274863251a35b1fbc92eb618b4 |
|
BLAKE2b-256 | 0c06b503e3247dd2f321c44417dca773a40484258d1ef4a22330fb175de9457b |
Hashes for django_throttle_requests-0.5.7-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 8f979015d12d1db1c595ec59dfee372fe92367801c5d07d68f6a1477a124faf9 |
|
MD5 | 10c68c9f39c00a297fc119b53ccc75b8 |
|
BLAKE2b-256 | 4856718ce13e356c48cf5bfb45909af973d7575aac411c25deca206ef79c9aea |