Created
July 19, 2013 10:54
-
-
Save Alir3z4/6038313 to your computer and use it in GitHub Desktop.
She's a Rock'nRoll Ruby! -- Simple IP blocker django middleware.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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