在Tomcat7上安装和运行CGI代理Python

在Tomcat7上安装和运行CGI代理Python,python,tomcat,web-applications,cgi,Python,Tomcat,Web Applications,Cgi,我想为openlayers设置一个在tomcat上运行的代理,因此我遵循以下步骤: 从OpenLayers网站下载了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

我想为openlayers设置一个在tomcat上运行的代理,因此我遵循以下步骤:

  • 从OpenLayers网站下载了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
    
  • 我的tomcat端口为6901,因此我修改了proxy.cgi文件,将我的域包括在allowedHosts列表中:

    allowedHosts=['localhost:6901']

  • 我已将proxy.cgi文件复制到以下文件夹:

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

  • 通过在下面的文件中添加节,修改了web应用程序的web.xml文件

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

  • 
    cgi
    org.apache.catalina.servlets.CGIServlet
    调试
    0
    cgiPathPrefix
    WEB-INF/cgi
    可执行
    c:\python25\python.exe
    密码环境
    真的
    5.
    cgi
    /cgi bin/*
    
    注释:“executable”参数的“param value”必须包含Pyhton安装的路径。(确实如此!)

  • 通过添加以下元素修改了我的web应用程序的context.xml文件,文件位于
    $TOMCAT_PATH$/webapps/myApp/META-INF/context.xml

  • 重新启动的雄猫

  • 要将代理与OpenLayers一起使用,请在代码中包含这一行:

    OpenLayers.ProxyHost=“/yourWebApp/cgi-bin/proxy.cgi?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”]有关,但我不知道它是如何关联的。

    问题似乎出现在您的
    allowedHosts
    行中。它应该包括您希望通过代理连接到的主机(例如,
    allowedHosts=['labs.metacarta.com']