view limiter for django
https://github.com/mymusise/django-view-limiter.git
Django-View-Limiter ===
Django-View-Limiter is a django app, which limit django view by IP,UA or user. Also you can limit view directly.
Install Requires ===
Install ===
Django-View-Limiter is best installed via PyPI. To install the latest version, run:
pip install apilimiter
or Install from github source:
pip install git+git://github.com/mymusise/django-view-limiter
Starting ===
Adding apilimiter in you your_project/settings.py
INSTALLED_APPS = ( '...', 'apilimiter', )
Then run migrate
python manage.py migrate apilimiter
If you want to save the result every day, you should install a crontab like this:
>crontab -e 0 1 * cd /path/of/your/project/ && python manage.py collectlimiter >> limiter.log
Examples ===
Example is the best and fastest way to start.
You can call the limiter decorator directly to wrap your view function like that. myapp/views.py
from django.shortcuts import HttpResponse from apilimiter.decorators import limiter
@limiter(key='',times=5) def index(request): return HttpResponse("decorators test")
If you view this api more than 5 times in a day, it will return the 403 page.
myapp/views.py
from django.shortcuts import HttpResponse from apilimiter.decorators import limiter
@limiter(key="META['REMOTE_ADDR']",times=5, redirect='/myapp/wrong') def index(request): return HttpResponse("decorators test")
What I want to limit first! But if you want to do this, you should call some authentication first. Let's take an example of django.contrib.auth.
myapp/views.py
from django.shortcuts import HttpResponse from apilimiter.decorators import limiter from django.contrib.auth.decorators import login_required
@loginrequired(loginurl="/admin/login/") @limiter(key='user.id',times=5, redirect='/myapp/wrong') def index(request): return HttpResponse("decorators test")
Also you can define redirect='/...' to return a special url to replace the 403 page.
myapp/views.py
from django.shortcuts import HttpResponse from apilimiter.decorators import limiter
@limiter(key='',times=5, redirect='/myapp/wrong') def index(request): return HttpResponse("decorators test")
myapp/urls.py
from django.conf.urls import url, include from myapp import views
urlpatterns = [ url(r'^$', views.index), url(r'^wrong$', views.wrong), ]
myapp/views.py
@limiter(key='',times=5, redirect=wrong) def index(request): return HttpResponse("decorators test")
def wrong(request): return HttpResponse("You can view the page any more!")
You can control the rate with define rate = 24 \ 60 \ 60 (s) Here's the example which limit the index view 30 times per minute.
@limiter(key='',times=30, rate=60) def index(request): return HttpResponse("decorators test")
from django.views.generic import View from apilimiter.mixin import LimiterMixin from django.shortcuts import HttpResponse
class MyView(LimiterMixin, View): times = 5 key = 'user.id' redirect = '' rate = 60
def get(self, request): return HttpResponse('Mixin test')