Skip to content

Instantly share code, notes, and snippets.

@kesor
Created April 15, 2012 14:44
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save kesor/2393220 to your computer and use it in GitHub Desktop.
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