User:Emily/Prototyping/Trimester 03/03

From XPUB & Lens-Based wiki

CGI Basics

understanding--> set up the HTTP server so that whatever a file in a certain directory is requested that file is not sent back instead it is executed as a program, and whatever that program outputs is sent back for your browser to display("Content-Type: text/html").
CGI programs can be python script, shell script, perl, c or c++ program ...
Get Method
url-->
 "content-type: text/html\r\n\r\n" 
print "<h2> Hello %s %s</h2>" % (first_name, last_name)
form-->
<form action="..." method="get"> 
<input type="text" name="..." /> 
<input type="submit" value="submit" /> 
</form>
Post Method (send information as separate message instead of a text string)
form-->
<form action="..." method="post">... </form>
checkbox-->
<form action="..." method="post" target="_blank">
<input type="checkbox" name="..." value="on"/> 
<input type="submit" value="submit" /> 
</form>
radio button-->

<form action="..." method="post" target="_blank"> <input type="radio" name="..." value="on"/> <input type="submit" value="submit" /> </form> </source>

TextArea-->
<form action="..." method="post" target="_blank">
<textarea name="textcontent" cols="40" rows="4"/> type your text here </textarea><input type="submit" value="submit" /> 
</form>
drop down box-->
<form action="/cgi-bin/dropdown.py" method="post" target="_blank">
<select name="dropdown">
<option value="Maths" selected>Maths</option>
<option value="Physics">Physics</option>
</select>
<input type="submit" value="Submit"/>
</form>
CGI
#!/usr/bin/python

# Import modules for CGI handling 
import cgi, cgitb 

# Create instance of FieldStorage 
form = cgi.FieldStorage() 

# Get data from fields
if form.getvalue('dropdown'):
   subject = form.getvalue('dropdown')
else:
   subject = "Not entered"

print "Content-type:text/html\r\n\r\n"
print "<html>"
print "<head>"
print "<title>Dropdown Box - Sixth CGI Program</title>"
print "</head>"
print "<body>"
print "<h2> Selected Subject is %s</h2>" % subject
print "</body>"
print "</html>"
cookies
file upload
file download

HTTP server -- programs (CGI scripts) -- interface (user input submitted through HTML<form>or<isindex>element) --> generate web content

python -m CGIHTTPSever

structure of CGI
web server, processes requests via HTTP (Hypertext Transfer Protocol)
process user input submitted through an HTML <FORM> or <ISINDEX> element
cgi_directories [' /cgi-bin', ' /htbin'] containing CGI scripts (server's special cgi-bin directory)
output - CGI script consists of two sections, separated by a blank line.
print "Content-Type: text/html" # header, telling what kind of data is following (HTML is following)
print                                          # blank line, end of headers
print "<TITLE>CGI script output</TITLE>"
print "Hello Morld"  #html, nice formatting
using cgi module
import cgitb
cgitb.enable() # display detailed reports in web browser
cgitb.enable(display=0, logdir="/path/to/logdir") #have the reports saved to files

= to get at submitted form data

FieldStorage class --> can be indexed like a Python dictionary (in operator; support the standard dictionary method keys(), and built-in function len())

form = cgi.FieldStorage()

the following code checks that the fields are set to a non-empty string

 if "***" not in form or "###" not in form:
     print "Error"
     print "please fill in *** and ### fields"
     return
 print "<p>***:", form["***"].value
 print "<p>###:", form["###"].value

if the submitted form data contains more than one field with the same name -->form[key] is not a FieldStorage or MiniFieldStorage instance but a list of such instances; form.getvalue(key) return a list of strings; getlist()method, always returns a list of values

value = form.getlist("username")
usernames = ",".join(value)

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

https://pzwiki.wdka.nl/mediadesign/How_am_I_being_served