User:Emily/Prototyping/Trimester 03/03

From XPUB & Lens-Based wiki
< User:Emily
Revision as of 11:24, 28 April 2015 by Emily (talk | contribs)

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