Created
April 15, 2012 14:44
-
-
Save kesor/2393220 to your computer and use it in GitHub Desktop.
Django X-Host-Name + X-Host-IP +X-Host-MacAddress 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
class HostnameMiddleware(object): | |
"""Middleware that adds a header X-Hostname to erroneous pages.""" | |
def process_response(self, request, response): | |
try: | |
if response.status_code != 200: | |
import socket | |
hostname = socket.gethostname() | |
addresses = ",".join( socket.gethostbyname_ex(hostname)[2] ) | |
response['X-Host-Name'] = hostname | |
response['X-Host-IP'] = addresses | |
except: | |
pass | |
return response | |
class MacAddressMiddleware(object): | |
"Middleware that adds a header X-Host-MacAddress to identify a host.""" | |
def process_response(self, request, response): | |
try: | |
import uuid | |
response['X-Host-MacAddress'] = hex(uuid.getnode()) | |
except: | |
pass | |
return response |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment