Search code examples
djangodjango-viewsddos

How to create a DDOS protection for a webpage inside of the views


So recenty I have been working on a Django app for a school project, and some friends decided it would be funny to DDOS my webpage. I am currently looking for new friends and a way to protect against this. I am currently trying to use a decorator limits from the library. It does block when getting ddos attacked but the problem is it shuts down the whole website, giving the error RateLimitException, which i do not want, for obvious reasons. I want the program to be able to block that specific ip address sending the requests, how could i do this. Here is the imports in views with only the homepage.

import ratelimit
from ratelimit import limits
@limits(calls=5, period=10)
def home_page(request):
  request.session.get("user_id")
  if request.session.get('user_id') is not None:

    authorized=True
    return render(request,template_name="index.html",context={"authorize":authorized})
    
  else:
    return render(request,template_name="index.html")

. . . . . . . . . . . .


Solution

  • You can use django-ratelimit. The ratelimit decorator allows you to block requests from IP if the number of requests is higher than rate.

    from django_ratelimit.decorators import ratelimit
    
    @ratelimit(key='ip', rate='15/m', block=True)
    def home_page(request):
        ...