Skip to content

Instantly share code, notes, and snippets.

@Alir3z4
Created July 19, 2013 10:54
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save Alir3z4/6038313 to your computer and use it in GitHub Desktop.
Save Alir3z4/6038313 to your computer and use it in GitHub Desktop.
She's a Rock'nRoll Ruby! -- Simple IP blocker django middleware.
from django.conf import settings
from django import http
def get_client_ip(request):
x_forwarded_for = request.META.get('HTTP_X_FORWARDED_FOR')
if x_forwarded_for:
ip = x_forwarded_for.split(',')[0]
else:
ip = request.META.get('REMOTE_ADDR')
return ip
class BlockedIpMiddleware(object):
def process_request(self, request):
if get_client_ip(request) in settings.BLOCKED_IPS:
return http.HttpResponseForbidden("She's a Rock'nRoll Ruby!")
return None
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment