Skip to content

Instantly share code, notes, and snippets.

@zehome
Created August 12, 2013 18:53
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zehome/6213872 to your computer and use it in GitHub Desktop.
Save zehome/6213872 to your computer and use it in GitHub Desktop.
Django reverse proxy for graphite
# Requirement: httplib2
import httplib2
class Httplib2ProxyMixin(object):
def _proxy(self, method, uri, body=None):
assert(method in ("GET", "POST"))
http = httplib2.Http()
resp, content = http.request(uri, method,
headers={'cache-control': 'no-cache'},
body=body)
httpresponse = HttpResponse(content,
content_type=resp["content-type"],
status=resp["status"])
httpresponse["Content-Length"] = resp["content-length"]
return httpresponse
class GraphiteProxyView(View, Httplib2ProxyMixin):
def _geturl(self, path):
return urlparse.urljoin(settings.GRAPHITE_URL, path)
def get(self, request, path):
return self._proxy("GET", self._geturl(path))
def post(self, request, path):
body = request.POST.urlencode()
return self._proxy("POST", self._geturl(path), body=body)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment