Joan Natalie Joan Natalie - 1 year ago 156
Python Question

Using Openlayers proxy.cgi with Fedora?

I am trying to move my application into Fedora server from my Windows server. My problem is that I use proxy.cgi to get information from geoserver.
How should I edit proxy.cgi after I move it into Fedora?

I have tried


and it still only return plain text.

This is my proxy.cgi code:

#!/usr/bin/python -u

"""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

# Designed to prevent Open Proxy type stuff.

allowedHosts = ['','', '',
'', '',
'', '',
'', '',
'', '',
'', '',
'', '', '', '','localhost',

'', '']

method = os.environ["REQUEST_METHOD"]

if method == "POST":
qs = os.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:
print "Status: 502 Bad Gateway"
print "Content-Type: text/plain"
print "This proxy does not allow you to access that location (%s)." % (host,)
print os.environ

elif url.startswith("http://") or url.startswith("https://"):

if method == "POST":
length = int(os.environ["CONTENT_LENGTH"])
headers = {"Content-Type": os.environ["CONTENT_TYPE"]}
body =
r = urllib2.Request(url, body, headers)
y = urllib2.urlopen(r)
y = urllib2.urlopen(url)

# print content type header
i =
if i.has_key("Content-Type"):
print "Content-Type: %s" % (i["Content-Type"])
print "Content-Type: text/plain"


print "Content-Type: text/plain"
print "Illegal request."

except Exception, E:
print "Status: 500 Unexpected Error"
print "Content-Type: text/plain"
print "Some unexpected error occurred. Error text was:", E

Answer Source

Your webserver needs to be setup to execute .cgi scripts.


Example (Apache:);

ScriptAlias /cgi-bin/ /usr/local/apache2/cgi-bin/

I should also point out that CGI is not really the recommend approach in Python. Please consider one of the following:

As noted by the OP's comments below (no sudo/root access to the server) your only other option is to build a simple web app with a suitable web framework and wrap the CGI using something like wsgi2cgi

Here is an example using circuits.web


#!/usr/bin/env python

print "Contnt-Type: text/html"
print "Hello World!" (server):

#!/usr/bin/env python

from wsgi2cgi import CGI

from circuits.web import Server
from circuits.web.wsgi import Gateway

def app(environ, start_response):
    wrapper = CGI("hello.cgi")
    return wrapper.application(environ, start_response)

server = Server(("", 5000))
Gateway({"/": app}).register(server)

example output:

$ curl -q -o - http://localhost:5000/
Hello World!

This would not require root/sudo access to the server; but you are restricted to running on non-priveleged ports (>1024).

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download