User:Emily/Prototyping/Trimester 03/03: Difference between revisions

From XPUB & Lens-Based wiki
(Created page with "web server -- programs (CGI scripts) --> interface -- generate the web content python -m CGIHTTPSever CGIHEEPSever module : cgi_directories [' /cgi-bin', ' /htbin'] contain...")
 
No edit summary
Line 3: Line 3:
  python -m CGIHTTPSever
  python -m CGIHTTPSever


CGIHEEPSever module
web server -- programs (CGI scripts) --> interface -- generate the web content
 
:process user input submitted through an HTML <FORM> or <ISINDEX> element
: cgi_directories [' /cgi-bin', ' /htbin'] containing CGI scripts
: cgi_directories [' /cgi-bin', ' /htbin'] containing CGI scripts
:
: CGI script consists of two sections, separated by a blank line.
print "Content-Type: text/html" #header, telling what kind of data is following
print "<TITLE>CGI script output</TITLE>"
print "Hello Morld"  #html, nice formatting
 
cgi module
import cgitb
cgitb.enable()
dispaly detailed reports in the web browser if any errors occur, can be removed later
 
FieldStorage class
form = cgi.FieldStorage()
 
If a field represents an uploaded file
fileitem = form["userfile"]
 
The appropriate way to read form data values was to always use the code which checks whether the obtained value is a single value or a list of values.
 
compact code:
<source lang=python>
import cgi
form = cgi.FieldStorage()
user = form.getfirst("user", "").upper()    # This way it's safe.
for item in form.getlist("item"):
    do_something(item)
</source>
 
Link: https://docs.python.org/2/library/cgi.html

Revision as of 17:13, 27 April 2015

web server -- programs (CGI scripts) --> interface -- generate the web content

python -m CGIHTTPSever

web server -- programs (CGI scripts) --> interface -- generate the web content

process user input submitted through an HTML <FORM> or <ISINDEX> element
cgi_directories [' /cgi-bin', ' /htbin'] containing CGI scripts
CGI script consists of two sections, separated by a blank line.
print "Content-Type: text/html" #header, telling what kind of data is following
print "<TITLE>CGI script output</TITLE>"
print "Hello Morld"  #html, nice formatting

cgi module

import cgitb
cgitb.enable()

dispaly detailed reports in the web browser if any errors occur, can be removed later

FieldStorage class

form = cgi.FieldStorage()

If a field represents an uploaded file

fileitem = form["userfile"]

The appropriate way to read form data values was to always use the code which checks whether the obtained value is a single value or a list of values.

compact code:

import cgi
form = cgi.FieldStorage()
user = form.getfirst("user", "").upper()    # This way it's safe.
for item in form.getlist("item"):
    do_something(item)

Link: https://docs.python.org/2/library/cgi.html