![]() |
|||
| HSG |
|
Grundlagen zur CGI-Programmierung mit Python
Bei der Entwicklung des Scripts pgtest1.py tauchten kleine Fallstricke auf. So darf anscheinend die Stylesheet-Datei nicht im Verzeichnis cgi-bin liegen, wenn der Browser darauf zugreifen soll. Die benutzte Datenbank ist in utf-8 codiert, daher wurde auch charset=utf-8 gewählt. Das hat zur Folge, dass schon ein falsch codiertes 'ä' einen 'Internal Server Error' auslöst.
#!/usr/bin/env python
import cgitb
cgitb.enable()
import psycopg2 as pg
conn = pg.connect("dbname=terra1 host=localhost user=gxx password=geheim")
cur = conn.cursor()
cur.execute("SELECT * FROM land WHERE kontinent LIKE 'Europ%' ORDER BY name")
rows = cur.fetchall()
print 'Content-type: text/html\n\n'
# mehrzeilige Ausgabe
print """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
<head>
<title>Informatik - Python - CGI - Testzugriff auf postgreSQL-Datenbank</title>
<meta name="author" content="mk" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../style.css" />
</head>
<body>
""" # Ende der mehrzeiligen Ausgabe
print '<h1>Europäische Länder aus terra1</h1>'
print '<table class="tab1">'
nr = 1
for z in range(len(rows)):
print ' <tr>'
print ' <td>',nr,'</td>'
nr = nr + 1
for s in range(len(rows[z])):
print ' <td>',rows[z][s],'</td>'
print ' </tr>'
print '</table>'
print """
</body>
</html>
"""
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de"> <head> <title>Informatik - Datenbanken - PostgreSQL - Python - CGI - Einloggen</title> <meta name="author" content="mk" /> <meta http-equiv="content-type" content="text/html; charset=iso-8859-1" /> </head> <body> <h3>Mini-Client</h3> <form action="../cgi-bin/miniclient.py" method="post"> <table> <tr><td>Benutzername:</td><td><input type="text" name="user" size="15" /></td></tr> <tr><td>Passwort:</td><td><input type="password" name="passwd" size="15" /></td></tr> <tr><td>SQL-Anweisung:</td><td><input type="text" name="sql" size="100" /></td></tr> <tr><td></td><td><input type="submit" value="Login" /></td></tr> </table> </form> </body> </html>

#!/usr/bin/env python
import cgi, cgitb
cgitb.enable()
form = cgi.FieldStorage()
user = form.getvalue('user')
passwd = form.getvalue('passwd')
sql = form.getvalue('sql')
fehler = False
from psycopg2 import *
try:
conn = connect('dbname='+user+' host=localhost user='+user+' password='+passwd)
except:
fehler = True;
if not fehler:
cur = conn.cursor()
cur.execute(sql)
rows = cur.fetchall()
# Ausgabe
print 'Content-type: text/html\n\n'
print """
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="de">
<head>
<title>Informatik - Python - CGI - Login-Verarbeitung</title>
<meta name="author" content="mk" />
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="../style.css" />
</head>
<body>
"""
if not fehler:
print '<table class="tab1">'
for z in range(len(rows)):
print ' <tr>'
for s in range(len(rows[z])):
print ' <td>',rows[z][s],'</td>'
print ' </tr>'
print '</table>'
else:
print '<p>Fehler beim Einloggen!</p>'
print """
</body>
</html>
"""