85458

Installing and Running CGI Proxy Python on Tomcat 7

I want to set up a proxy running on tomcat for openlayers, so I followed these steps:

<ol> <li>Downloaded the proxy.cgi file from the OpenLayers web site: </li> </ol>

http://trac.osgeo.org/openlayers/browser/trunk/openlayers/examples/proxy.cgi

Here is the code:

#!c:/Python27/python.exe """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 = ['www.openlayers.org', 'openlayers.org', 'labs.metacarta.com', 'world.freemap.in', 'prototype.openmnnd.org', 'geo.openplans.org', 'sigma.openplans.org', 'demo.opengeo.org', 'www.openstreetmap.org', 'sample.azavea.com', 'v2.suite.opengeo.org', 'v-swe.uni-muenster.de:8080', 'vmap0.tiles.osgeo.org', 'www.openrouteservice.org','localhost:6901'] 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] else: url = "http://www.openlayers.org" else: fs = cgi.FieldStorage() url = fs.getvalue('url', "http://www.openlayers.org") try: host = url.split("/")[2] if allowedHosts and not host in allowedHosts: print "Status: 502 Bad Gateway" print "Content-Type: text/plain" print print "This proxy does not allow you to access that location (%s)." % (host,) print 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 = sys.stdin.read(length) r = urllib2.Request(url, body, headers) y = urllib2.urlopen(r) else: y = urllib2.urlopen(url) # print content type header i = y.info() if i.has_key("Content-Type"): print "Content-Type: %s" % (i["Content-Type"]) else: print "Content-Type: text/plain" print print y.read() y.close() else: print "Content-Type: text/plain" print print "Illegal request." except Exception, E: print "Status: 500 Unexpected Error" print "Content-Type: text/plain" print print "Some unexpected error occurred. Error text was:", E <ol> <li>

I have my tomcat at port 6901, so I modified the proxy.cgi file to include my domain in the allowedHosts list:

allowedHosts = ['localhost:6901']

</li> <li>

I copied the proxy.cgi file to the following folder:

$TOMCAT_PATH$/webapps/myApp/WEB-INF/cgi/

</li> <li>

Modified the file web.xml of the web app by adding the sections below the file at

$TOMCAT_PATH$/webapps/myApp/WEB-INF/web.xml

</li> </ol>
<servlet> 
    <servlet-name>cgi</servlet-name> 
    <servlet-class>org.apache.catalina.servlets.CGIServlet</servlet-class> 
    <init-param> 
        <param-name>debug</param-name> 
        <param-value>0</param-value> 
    </init-param> 
    <init-param> 
        <param-name>cgiPathPrefix</param-name> 
        <param-value>WEB-INF/cgi</param-value> 
    </init-param> 
    <init-param> 
        <param-name>executable</param-name> 
        <param-value>c:\python25\python.exe</param-value> 
    </init-param> 
    <init-param> 
        <param-name>passShellEnvironment</param-name> 
        <param-value>true</param-value> 
    </init-param> 
    <load-on-startup>5</load-on-startup> 
</servlet> 

<servlet-mapping> 
<servlet-name>cgi</servlet-name> 
<url-pattern>/cgi-bin/*</url-pattern> 
</servlet-mapping>


Comment: the “param-value” for the “executable” parameter has to contain the path to your Pyhton installation. (it does!)

<ol> <li>

Modified the file context.xml of my web app by adding the element below, file at $TOMCAT_PATH$/webapps/myApp/META-INF/context.xml

</li> <li>

Restarted Tomcat

</li> <li>

To use the proxy with OpenLayers, included this single line into the code:

OpenLayers.ProxyHost = "/yourWebApp/cgi-bin/proxy.cgi?url=";

</li> </ol>

But the proxy isn't working! When I try to use it like:

/myApp/cgi-bin/proxy.cgi?url=labs.metacarta.com

I get this error:

Some unexpected error occurred. Error text was: list index out of range

I think it's related to os.environ["REQUEST_METHOD"], but I don't know how it's related.

Answer1:

The problem appears to be in your allowedHosts line. It should include the hosts that you want to connect to over the proxy (e.g. allowedHosts = ['labs.metacarta.com'])

Recommend

  • How do I to figure out where inserted (possibly malicious) JavaScript code is coming from
  • Wordpress pre_get_posts and passing arguments
  • Failing to parse JSON in Rails
  • Picking a random image out of different directories based on GEO locations
  • Why isnt this code working
  • How to prepare existing database for location aware searching?
  • Issue with geolocation timeout callback firing whether response received or not
  • IP address Mismatch between Azure Application Insights and Google Search
  • 400 bad request with ggmap
  • iPhone reverse geocoding: give all possible streets/suburbs?
  • ng-click not working in IE but works fine in CHROME
  • How to simulate speed on Android emulator?
  • geocomplete is not a function using autocomplete
  • Search Yahoo API geo.places with like operator
  • Repeating 503's messages when querying DBpedia
  • Capturing microphone input through HTML5 [closed]
  • DJ3S - Graticule removed when rotation is done in orthographic projection
  • Geocoder works in 1.6 but not in 2.2 emulator
  • How to get pixel coordinates from canvas polygon (filled area)
  • Geospatial Index for inner structure
  • How can I programmatically check whether geo-tagging is enabled?
  • Google Maps API v3 javascript Markers don't always load
  • how to use Openlayers map
  • Legend label errors with glmnet plot in R
  • Shortest route between multiple points in mapkit for iphone app
  • Geo Fix not working in Android SDK 2.2
  • How to move axis labels automatically in rgl R
  • Add Jpanel to Jframe NetBeans
  • How to convert Neo4j Result to GraphJSON
  • Recursion in ASP.NET Core Razor views
  • javascript variables, What does var x = a = {} do?
  • Making header act like google chrome mobile app address bar
  • Watson Conversation - Why is the ANYTHING ELSE node not chosen
  • Angular Bootstrap Carousel Slide Transition not working correctly
  • pickle.PicklingError: args[0] from __newobj__ args has the wrong class with hadoop python
  • Using $compile in a directive triggers AngularJS infinite digest error
  • How to get address from latitude and longitude android google map v2 [duplicate]
  • Where to put my custom functions in Wordpress?
  • How can I use Kendo UI with Razor?
  • CSS Applying specific rule for a specific monitor resolution with only CSS is posible?