Create a gist now

Instantly share code, notes, and snippets.

Django X-Host-Name + X-Host-IP +X-Host-MacAddress middleware
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