1609
Comment:
|
2208
Added notes section
|
Deletions are marked like this. | Additions are marked like this. |
Line 19: | Line 19: |
Line 54: | Line 53: |
== Notes == Instead of checking if key exists and then using `form[key].value`, you can just use shortcut `form.getvalue(key, default)` For troubleshooting, you should include lines `import cgitb; cgitb.enable` to head of you CGI script. |
|
Line 61: | Line 66: |
* [https://www.devshed.com/index2.php?option=content&task=view&id=198&pop=1&page=0&hide_js=1 Writing CGI Scripts in Python] * [https://www.voidspace.org.uk/python/cgi.shtml Voidspace Python CGI collection] - Working Python CGI scripts to use and/or study |
|
Line 64: | Line 71: |
(none yet!) | We need a good python CGI framework - Sridhar R Nevow and [https://srid.bsdnerds.org/hacking/wallaby/ Wallaby] |
CGI Scripts
The [https://python.domainunion.de/doc/current/lib/module-cgi.html cgi module] is at the core of the Python CGI scripts.
Basically, you just need to print out an HTTP header ("Content-type: text/html"), a web page, and handle any forms you may have received.
Getting Apache's permissions just right can be annoying, and is sadly beyond this page's scope.
Sample Code
1 #!/usr/bin/env python
2
3 import cgi
4
5 print "Content-type: text/html"
6 print
7
8 print """
9 <html>
10
11 <head><title>Sample CGI Script</title></head>
12
13 <body>
14
15 <h3> Sample CGI Script </h3>
16 """
17
18 form = cgi.FieldStorage()
19 if form.has_key( "message" ):
20 message = form["message"].value
21 else:
22 message = "(no message)"
23
24 print """
25
26 <p>Previous message: %s</p>
27
28 <p>form:</p>
29
30 <form method="post" action="index.cgi">
31 <p>message: <input type="text" name="message"/></p>
32 </form>
33
34 </body>
35
36 </html>
37 """ % message
Notes
Instead of checking if key exists and then using form[key].value, you can just use shortcut form.getvalue(key, default)
For troubleshooting, you should include lines import cgitb; cgitb.enable to head of you CGI script.
See Also
[https://python.domainunion.de/doc/current/lib/module-cgi.html cgi module documentation]
[https://python.domainunion.de/doc/current/lib/module-Cookie.html Cookie module documentation]
[https://www.cs.virginia.edu/~lab2q/lesson_7/ python CGI tutorial] - w/ hints about maintaining sessions either through forms or through cookies
[https://gnosis.cx/publish/programming/feature_5min_python.html python CGI tutorial] - w/ hints about printing out tracebacks
[https://python.domainunion.de/doc/current/lib/internet.html other internet protocol module documentation]
[https://www.devshed.com/index2.php?option=content&task=view&id=198&pop=1&page=0&hide_js=1 Writing CGI Scripts in Python]
[https://www.voidspace.org.uk/python/cgi.shtml Voidspace Python CGI collection] - Working Python CGI scripts to use and/or study
Discussion
- We need a good python CGI framework - Sridhar R
Nevow and [https://srid.bsdnerds.org/hacking/wallaby/ Wallaby]