这是配置 Apache2:
http://narnia.cs.ttu.edu/drupal/node/43
添加
ScriptAlias /cgi-bin/ /var/www/cgi-bin/
<Directory "/var/www/cgi-bin">
AllowOverride None
Options +ExecCGI -MultiViews +SymLinksIfOwnerMatch
Order allow,deny
Allow from all
AddHandler cgi-script .py # tell Apache to handle every file with .py suffix as a cgi program
AddHandler default-handler .html .htm # tell Apache to handle HTML files in regular way
</Directory>
到 /etc/apache2/sites-available/default
这个代码片段:
http://wiki.python.org/moin/CgiScripts ...(感谢 Mike Christensen 提供此链接)
1 #!C:\Python27\python.exe -u
2 #!/usr/bin/env python
3
4 import cgi
5 import cgitb; cgitb.enable() # for troubleshooting
6
7 print "Content-type: text/html"
8 print
9
10 print """
11 <html>
12
13 <head><title>Sample CGI Script</title></head>
14
15 <body>
16
17 <h3> Sample CGI Script </h3>
18 """
19
20 form = cgi.FieldStorage()
21 message = form.getvalue("message", "(no message)")
22
23 print """
24
25 <p>Previous message: %s</p>
26
27 <p>form
28
29 <form method="post" action="index.cgi">
30 <p>message: <input type="text" name="message"/></p>
31 </form>
32
33 </body>
34
35 </html>
36 """ % cgi.escape(message)
结合起来,在没有数据库的情况下准确地给我我想要的东西,但实现是合乎逻辑的。
这是下一步。
http://www.java2s.com/Tutorial/Python/0440__CGI-Web/Loginform.htm
您的 index.htm:
<HTML>
<HEAD><TITLE>Login Page</TITLE></HEAD>
<BODY>
<CENTER>
<FORM method="POST" action="http://yourserver/cgi-bin/login.py">
<paragraph> Enter your login name: <input type="text" name="login">
<paragraph> Enter your password: <input type=password name="password">
<paragraph> <input type="submit" value="Connect">
</FORM>
</CENTER>
<HR>
</form>
</BODY>
</HTML>
您的 login.py CGI:
#!/usr/local/bin/python
import cgi
def header(title):
print "Content-type: text/html\n"
print "<HTML>\n<HEAD>\n<TITLE>%s</TITLE>\n</HEAD>\n<BODY>\n" % (title)
def footer():
print "</BODY></HTML>"
form = cgi.FieldStorage()
password = "python"
if not form:
header("Login Response")
elif form.has_key("login") and form["login"].value != "" and form.has_key("password") and form["password"].value == password:
header("Connected ...")
print "<center><hr><H3>Welcome back," , form["login"].value, ".</H3><hr></center>"
print r"""<form><input type="hidden" name="session" value="%s"></form>""" % (form["login"].value)
print "<H3><a href=browse.html>Click here to start browsing</a></H3>"
else:
header("No success!")
print "<H3>Please go back and enter a valid login.</H3>"
footer()