Skip to content

Instantly share code, notes, and snippets.

@dangerouse
Last active September 29, 2015 10:17
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 dangerouse/1586071 to your computer and use it in GitHub Desktop.
Save dangerouse/1586071 to your computer and use it in GitHub Desktop.
Cloudsponge Proxy Reference - Python & Django
# Cloudsponge proxy reference
# Python reference implementation using django
#
# contributed by Dannie Chu of foodoro.com
from django.http import HttpResponse, HttpResponseRedirect
# from fdr.logging.logger import Logger
from urllib2 import HTTPRedirectHandler
import urllib
import urllib2
class NoRedirectHandler(HTTPRedirectHandler):
def http_error_302(self, req, fp, code, msg, headers):
infourl = urllib.addinfourl(fp, headers, req.get_full_url())
infourl.status = code
infourl.code = code
return infourl
http_error_300 = http_error_301 = http_error_303 = http_error_307 = http_error_302
def cloudspongeProxy(request):
httpResponse = HttpResponse()
data = {}
if request.method == 'GET' :
data = request.GET
elif request.method == 'POST' :
data = request.POST
if data :
data = urllib.urlencode(data)
try :
proxyRequest = urllib2.Request("https://api.cloudsponge.com/auth")
opener = urllib2.build_opener(NoRedirectHandler())
response = opener.open(proxyRequest, data, timeout=30)
if response.getcode() == 302:
headers = dict(response.info())
httpResponse = HttpResponseRedirect(headers['location'])
except Exception, ex :
# logger.exception("failed to proxy request: %s" % str(ex))
return httpResponse
@sohan
Copy link

sohan commented Apr 21, 2014

This gist previously worked perfectly for me. Recently, for some reason the response header from api.cloudsponge.com did not have a status field, so I had to change line 38 to:

if headers.get('location'):

And then everything works as before.

@dangerouse
Copy link
Author

I've updated the gist so that it doesn't depend on the 'status' header now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment