Seite 1 von 1

Python-Skript auf Debian-Server zum Laufen bringen

Verfasst: Mittwoch 4. Februar 2009, 13:07
von Kai-Behncke
Hallo liebe Leute,

ich bin blutiger Python-Anfänger und stehe vor einem Problem.

Ich habe folgendes Skript welches ich gerne auf meinem Debian-System zum Laufen bringen möchte:

Code: Alles auswählen

#!/usr/bin/python


"""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', 
                'www.bsc-eoc.org', 'geo.openplans.org',
                'sigma.openplans.org',
                'www.openstreetmap.org']

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

Auf Windows (naütlich mit verändertem Pfad) läuft das auch wunderbar, nur wenn ich das auf meinem server unter Debian aufrufe

http://mein_server/cgi-bin/proxy.cgi ....dann passiert garnichts.

Woran könnte das liegen?
Wie lönnte ich denn zumindest bei einem Web-Cgi-Aufruf eine Fehlermdeldung erhalten?


Vielen Dank im Voraus, Kai

Verfasst: Mittwoch 4. Februar 2009, 14:38
von Leonidas
Hallo Kai-Behncke, willkommen im Forum,

Füge diesen Code nach den Importzeilen ein:

Code: Alles auswählen

import cgitb
cgitb.enable()

Verfasst: Mittwoch 4. Februar 2009, 14:52
von Mephisto
Hi,

kann auch sein dass du vergessen hast die Datei ausführbar zu machen, soweit ich weis muss das bei CGI-Scripts so sein (Geht mit chmod +x /pfad/zur/datei in der Konsole)
Ansonsten würde ich eventuell mal ein Blick in die error.log deines Webservers schauen, da steht sicher etwas drin.

lg mephisto

Verfasst: Mittwoch 4. Februar 2009, 16:18
von Kai-Behncke
Vielen Dank zunächst einmal.

Ich habe

Code: Alles auswählen

import cgitb
cgitb.enable()
eingetragen, lieider keine Veränderung.


Der Apache gibt mir folgende Fehlermeldung aus:

[Wed Feb 04 16:08:41 2009] [error] [client 131.173.78.250] (2)No such file or directory: exec of '/etc/apache2/cgi-bin/proxy2.cgi' failed
[Wed Feb 04 16:08:41 2009] [error] [client 131.173.78.250] Premature end of script headers: proxy2.cgi
[Wed Feb 04 16:10:03 2009] [error] [client x.x.x.x] client sent HTTP/1.1 request without hostname (see RFC2616 section 14.23): /w00tw00t.at.ISC.SANS.DFind:)


.....was ich nicht verstehe.Das Skript steht auf 777
und ist defenitiv vorhanden, und wie gesagt, wenn ich es lokal ausführe (unter Windows) dann ist alles ok???

Hat jemand vielleicht noch einen Tip?

Verfasst: Mittwoch 4. Februar 2009, 16:36
von Rebecca
Premature end of script headers: proxy2.cgi
Eventuell gibt's ein Problem mit Windows-Zeilenumbruechen? IMO stoert sich Linux daran, wenn in der shebang ein solcher vorkommt. Also mal dos2unix oder so drueberlaufen lassen.

Verfasst: Donnerstag 5. Februar 2009, 08:57
von Kai-Behncke
Spitze!!!

Dos2unix war die Lösung. Vielen Dank!!!!