Titivillus/titivillus/middleware.py

21 lines
478 B
Python
Raw Normal View History

2018-08-30 13:33:08 -04:00
#!/usr/bin/env python3
"""
Some custom middleware for titivillus.
"""
class XRealIPMiddleware(object):
"""
Attempts to set the get the visitor's real IP by grabbing the
`X-Real-IP` header from the request.
"""
def __init__(self, get_response):
self.get_response = get_response
def __call__(self, request):
try:
real_ip = request.META['HTTP_X_REAL_IP']
except KeyError:
pass
else:
request.META['REMOTE_ADDR'] = real_ip
return self.get_response(request)