


























Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
This page cannot be seen from the preview
Don't miss anything!



























Web Server Programming
How a web server passes information to a program How that program passes information back to the web server
A particular language (^) You can use Fortran, the shell, C, Java, Perl, Python… How the web server figures out what program to run (^) Each web server has its own rules (^) We'll (briefly) talk about Apache's
Figure 14.1: CGI Data Processing Cycle
(^) Which means that the CGI program is responsible for creating headers
By default, modern servers won't do this unless they're told they can
(^) Clients and servers need a way to specify data types to each other (^) Remember, bytes are just bytes: the browser doesn't magically know how to interpret them
(^) Organizes data types into families, and provides a two-part name for each type (^) Use the "Content-Type" header to specify the MIME type of the data being sent application/pdf Adobe PDF document Application-specific data Apple Quicktime video format Video^ video/quicktime Audio audio/x-mp3 MP3 audio file Image image/jpeg JPEG-format image Text text/html Web pages Family Specific Type Describes
(^) Invoke it by going to http://www.yourserver.com/cgi- bin/hello_cgi.py (^) By convention, CGI programs are put in a cgi-bin directory (^) Browser displays the simple HTML page generated by the program Figure 14.2: Basic CGI Output
(^) But the whole point of CGI is to generate content dynamically (^) E.g., show a list of environment variables and their values #!/usr/bin/env python import os, cgi
print 'Content-type: text/html' print
print '' keys = os.environ.keys() keys.sort() for k in keys: print '
%s: %s
' % (cgi.escape(k), cgi.escape(os.environ[k])) print ''
Create a form using a … element (^) action attribute specifies the URL to send data to (^) method attribute specifies the type of HTTP request to send (^) Usually "POST" for HTML forms (^) Inside the form, can have: (^) elements to let users choose values from a list (^) List items specified using elements for other kind of data (^) If type is "text", get a one-line text entry box (^) If type is "checkbox", get an on/off checkbox (^) "submit" and "reset" create buttons to submit the form, or re-set the data to initial values
Each element has a name attribute (^) These become the names of the parameters that the client sends to the server (^) The input elements' values are the parameters' values Submitting the form shown above with default values produces: (^) os.environ['REQUEST_METHOD']: "POST" (^) os.environ['SCRIPT_NAME']: "/cgi-bin/simple_form.py" (^) os.environ['CONTENT_TYPE']: "application/x-www-form- urlencoded" (^) os.environ['REQUEST_LENGTH']: "80" (^) Standard input: sequence=GATTACA&search_type=Similarity +match&program=FROG-11&program=Bayes-Hart
(^) Read and parse environment variables (^) Read extra data from standard input (^) But the mechanics are the same each time, so use Python's cgi module instead (^) Defines a dictionary-like object called FieldStorage (^) Keys are parameter names (^) Values are either strings (if there's a single value assocatied with the parameter) or lists (if there are many) (^) When a FieldStorage object is created, it reads and stores information contained in the URL and environment (^) Which means that a CGI program should only ever create one (^) Program can read extra data from sys.stdin
(^) During development, add import cgitb; cgitb.enable() to the top of the program (^) cgitb is the CGI traceback module (^) When enabled, it will create a web page showing a stack trace when something goes wrong in your script Testing whether a FieldStorage value is a string or a list is tedious (^) In almost all cases, you'll know whether to expect one value or many (^) Use FieldStorage.getfirst(name) to get the unique value (^) Returns the first, if there are many (^) FieldStorage.getlist(name) always returns a list of values (^) Empty list if there's no data associated with name (^) If there's only one value, get a single-item list
(^) Often want to change the data a server is managing, as well as read it (^) Update a description of an experiment, change your preferred email address, etc.
(^) CGI program stuffs parameters from HTTP requests into SQL queries (^) Runs the queries (^) Translates results into HTML to send back to the client Figure 14.5: Three Tier Architecture