Skip to main content
 首页 » 编程设计

python之在 Fedora 中使用 Openlayers proxy.cgi

2025年05月04日8linjiqin

我正在尝试将我的应用程序从我的 Windows 服务器迁移到 Fedora 服务器。我的问题是我使用 proxy.cgi 从地理服务器获取信息。 proxy.cgi 迁移到 Fedora 后应该如何编辑?

我试过了

#!/usr/bin/python 

它仍然只返回纯文本。

这是我的 proxy.cgi 代码:

#!/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 = ['www.google.co.id','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', '172.20.32.11:8080', '172.20.32.11','localhost',  
 
'localhost:8080',  
                'http://192.168.64.2:8080', 'http://192.168.64.2'] 
 
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 

请您参考如下方法:

您的网络服务器需要设置为执行 .cgi 脚本。

参见:http://httpd.apache.org/docs/current/howto/cgi.html

例子(Apache:);

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

我还应该指出,CGI 并不是真正推荐的 Python 方法。请考虑以下其中一项:

正如 OP 在下面的评论中所指出的(没有对服务器的 sudo/root 访问权限) 您唯一的选择是使用合适的网络框架构建一个简单的网络应用程序 并使用类似 wsgi2cgi 的方式包装 CGI

这是一个使用 circuits.web 的例子

你好.cgi:

#!/usr/bin/env python 
 
print "Contnt-Type: text/html" 
print 
print "Hello World!" 

cgiwrapper.py(服务器):

#!/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(("0.0.0.0", 5000)) 
Gateway({"/": app}).register(server) 
server.run() 

示例输出:

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

这不需要 root/sudo 访问服务器; 但您只能在非特权端口上运行 (>1024)。