2012-08-05 47 views
2

我想建立在Tomcat的運行的OpenLayers的代理,所以我遵循下列步驟操作:安裝和運行CGI代理的Python在Tomcat 7

  1. 從的OpenLayers網站上下載的文件proxy.cgi:

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

下面是代碼:

#!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 
  1. 我有在端口6901的Tomcat,所以我修改了proxy.cgi文件以包括我在allowedHosts列表域:

    allowedHosts = [ '本地主機:6901']

  2. 我複製proxy.cgi文件到以下文件夾:

    $ $ TOMCAT_PATH/web應用/對myApp/WEB-INF/CGI/

  3. 通過在添加文件下面的部分修改的web應用程序的文件的web.xml

    $ TOMCAT_PATH $/webapps /目錄對myApp/WEB-INF/web.xml中

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

點評:「參數值」爲「可執行」參數必須包含您Pyhton路徑安裝。 (它!)

  1. 通過添加下面的元素,文件修改了我的web應用程序的文件context.xml中在$TOMCAT_PATH$/webapps/myApp/META-INF/context.xml

  2. 重啓動Tomcat的

  3. 要使用代理與OpenLayers,包括單行代碼:

    OpenLayers.ProxyHost =「/yourWebApp/cgi-bin/proxy.c ?GI URL =「;

但代理不工作!當我嘗試使用它像:

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

我得到這個錯誤:

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

我認爲這是os.environ [「REQUEST_METHOD」]有關,但我不知道它是如何的相關。

+0

任何人? pleaaaaaaaaaaaase – 2012-08-06 16:24:19

回答

2

該問題似乎在您的allowedHosts行中。它應該包括你想通過代理連接的主機(例如allowedHosts = ['labs.metacarta.com']