Skip to content

Instantly share code, notes, and snippets.

What would you like to do?
beginnings of a proxy for web2py. See for more
# coding: utf8
def index():
"""Based on
This is a blind proxy that we use to get around browser
restrictions that prevent the Javascript from loading pages not on the
same server as the Javascript. This has several problems: it's less
efficient, it might break some sites, and it's a security risk because
people can use this proxy to browse the web and possibly do bad stuff
with it. It only loads pages via http and https, but it can load any
content type. It supports GET and POST requests."""
import urllib2
import cgi
import sys, os
# prevent Open Proxy abuse
allowedHosts = ['', '',
'', '',
'', '',
'', '',
'', '',
method = request['wsgi'].environ['REQUEST_METHOD']
if method == "POST":
qs = request['wsgi'].environ["QUERY_STRING"]
d = cgi.parse_qs(qs)
if d.has_key("url"):
url = d["url"][0]
url = ""
fs = cgi.FieldStorage()
url = fs.getvalue('url', "")
host = url.split("/")[2]
if allowedHosts and not host in allowedHosts:
msg = "Status: 502 Bad Gateway\n"
msg += "Content-Type: text/plain\n\n"
msg += "This proxy does not allow you to access that location (%s).\n\n" % (host,)
msg += os.environ
return msg
elif url.startswith("http://") or url.startswith("https://"):
if method == "POST":
length = int(request['wsgi'].environ["CONTENT_LENGTH"])
headers = {"Content-Type": request['wsgi'].environ["CONTENT_TYPE"]}
body =
r = urllib2.Request(url, body, headers)
y = urllib2.urlopen(r)
y = urllib2.urlopen(url)
# print content type header
# TODO: this doesn't work in web2py, need to figure out how that happens?
#i =
#if i.has_key("Content-Type"):
# msg = "Content-Type: %s" % (i["Content-Type"])
# msg = "Content-Type: text/plain"
#msg += "\n" +
msg =
return msg
msg = "Content-Type: text/plain\n\n"
msg += "Illegal request."
return msg
except Exception, E:
msg = "Status: 500 Unexpected Error\n"
msg += "Content-Type: text/plain\n\n"
msg += "Some unexpected error occurred. Error text was:", E
return msg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment