在Tomcat 7上安装和运行CGI Proxy Python [英] Installing and Running CGI Proxy Python on Tomcat 7

查看:118
本文介绍了在Tomcat 7上安装和运行CGI Proxy Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想为tomcat设置运行在tomcat上的代理,所以我遵循以下步骤:


  1. 下载了proxy.cgi文件从OpenLayers网站上:

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



这里是代码:

 #!c:/Python27/python.exe 


这是一个盲代理,我们使用它来绕过浏览器
的限制,这些限制阻止Javascript将页面加载到与Javascript不在同一服务器上的
上。问题:
的效率较低,可能会破坏某些站点,并且存在安全风险,因为
的人可以使用此代理浏览Web并可能对
进行不良处理。通过http和https的页面,但它可以加载任何
内容类型。它支持GET和POST请求。

import urllib2
import cgi
import sys,os

#旨在防止Open Proxy类型的东西。

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,示例。 azavea.com,
v2.suite.opengeo.org, v-swe.uni-muenster.de:8080、
vmap0.tiles.osgeo.org、www。 openrouteservice.org','localhost:6901']

方法= os.environ [ REQUEST_METHOD]

如果方法== POST:
qs = os.environ [ QUERY_STRING]
d = cgi.parse_qs(qs)
如果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)

尝试:
host = url.split( /)[2]
如果allowedHosts而不是允许的主机主机:
pr int状态:502错误的网关
打印内容类型:文本/纯文字
打印
打印此代理不允许您访问该位置(%s)。 %(主机)
打印
打印os.environ

elif url.startswith( http://)或url.startswith( https://) :

如果方法== POST:
长度= int(os.environ [ CONTENT_LENGTH])
标头= { Content-Type:os.environ [ CONTENT_TYPE]}
body = sys.stdin.read(length)
r = urllib2.Request(URL,body,headers)
y = urllib2.urlopen(r)
否则:
y = urllib2.urlopen(url)

#打印内容类型标头
i = y.info()
如果i.has_key( Content-Type ):
打印内容类型:%s%(i [ Content-Type])
其他:
打印内容类型:文本/纯文字
打印

打印y.read()

y.close()
其他:
打印内容类型:文本/纯文本
打印
打印非法请求。

例外,E:
打印状态:500意外错误
打印 Content-Type:文本/纯文本
打印
打印发生了一些意外的错误。错误文本为:,E




  1. 我的tomcat位于端口6901,所以我修改了proxy.cgi文件,将我的域包含在allowedHosts列表中:



    allowedHosts = ['localhost:6901']


  2. 我将proxy.cgi文件复制到了以下文件夹:



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


  3. 通过在文件

    $下添加以下部分来修改Web应用的文件web.xml。 b
    $ b

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




< pre class = lang-xml prettyprint-override> < 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>可执行文件< / 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>
<启动时加载> 5< /启动时加载>
< / servlet>

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

注释: executable参数的 param-value必须包含指向您的路径Pyhton安装。 (确实如此!)


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




  2. 重新启动Tomcat


  3. 要在OpenLayers中使用代理,请在代码中包含以下一行:



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


但是代理不起作用!当我尝试使用它时:

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

我收到此错误:

 发生一些意外错误。错误文本是:列表索引超出范围

我认为它与os.environ [ REQUEST_METHOD有关],但我不知道它的关系。

解决方案

问题似乎出在您的 allowedHosts 行。它应包括您要通过代理连接的主机(例如 allowedHosts = [’labs.metacarta.com']


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

  1. Downloaded the proxy.cgi file from the OpenLayers web site:

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

  1. 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']

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

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

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

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

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

  2. Restarted Tomcat

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

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

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.

解决方案

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'])

这篇关于在Tomcat 7上安装和运行CGI Proxy Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆