CGI-Script

Andy Froemmel andy.froemmel at sap.com
Mon Feb 24 15:08:46 CET 2003


Sorry, my mistake...

Hallo,
I have running a HTTPCGI.py :

import BaseHTTPServer
import CGIHTTPServer


class Handler(CGIHTTPServer.CGIHTTPRequestHandler):
    cgi_directories = ["/cgi-bin"]

PORT = 8000

httpd = BaseHTTPServer.HTTPServer(("", PORT), Handler)
print "Service an Port", PORT
httpd.serve_forever()



furthermore I send data form a HTML-Side:



<html>
<head>
<title>Beispiel-Formular f&uuml;r Python</title>
</head>
<body bgcolor="#ffffdf" text="#000000" link="#0000ff"
vlink="#800080" alink="#ff0000">
<h2> Ein Beispiel-Formular (CGI in Python)</h2>
<form  action="/cgi-bin/trex.py" method="post"
name="beispiel">
<p>
Die erste GUID<br>
<input type="text"  size="70"  name="GUID1">
</p>
<p>
Die zweite GUID<br>
<input type="text"  size="70"  name="GUID2">
</p>
<br>
<p>
Der erste Host<br>
<input type="text"  size="20"  name="HOST1">
</p>
<br>
<p>
Der zweite Host<br>
<input type="text"  size="20"  name="HOST2">
</p>
<br>

<input type="SUBMIT" value="Abschicken">
<input type="RESET" value="Lieber doch nicht . . .">
</form>
</body>
</html>


to the following py-script:

import cgi
import sys
import traceback
fname = 'log_cgi.txt'
fname1 = 'error_cgi.txt'
def ausgabe(g1, g2, h1, h2):
    nachricht =''
    nachricht = g1 + g2 + h1 + h2 + '\n<br>\n'
    return nachricht

# main
sys.stderr = sys.stdout
print "Content-Type: text/html\n\n"

form=cgi.FieldStorage()


if form.has_key("GUID1") and form.has_key("GUID2") and
form.has_key("HOST1") and form.has_key("HOST2"):
    if form["GUID1"].value != "" and
form["HOST1"].value != "":
        form_ok = 1
        g1 =  form["GUID1"].value
        g2 =  form["GUID2"].value
        h1 =  form["HOST1"].value
        h2 =  form["HOST2"].value
        content = str(g1 + g2 + h1 + h2)
        file = open(fname, 'w')
        file.write(content)
        file.close()
        #print "Content-Type: text/html\n\n"
        print '</title>\n</head>\n<body
bgcolor="#ffffdf" text="navy">\n'
        #print ausgabe(g1, g2, h1, h2)
        print str(g1)
else:
    print "<html><head><title>\n"
    print "Python is calling you!\n"
    print '</title>\n</head>\n<body bgcolor="#ffffdf"
text="navy">\n'
    print 'da ist noch ein Fehler'
    #print ausgabe(g1, g2, h1, h2)
    print "</body>\n</html>\n\n"


The process is running (the data form the HTML-Side are
send to the py-script and written in an txt-file)
The problem is to get the response-side.
I mean, If I send the form to the script, I get
the message:
"The page cannot be displayed". If I make a refresh
of the html-side, the right information form the "else-function"
are shown.
Why does not send the script the information from the
form back to the HTML-side??

Thanks and best regards,
Andy








More information about the Python-list mailing list