To use a Python program as a CGI script, you must import the cgi module. For purposes of troubleshooting your script, you should also import the module cgitb. This provides alternative exception handlers which offer more informative error messages in the event of an exception. While you can write CGI scripts without cgitb, it is recommended for its helpful exception handling.
If you have trouble running your CGI scripts, remember to check your permissions and the setup of your Apache server. If you do not have administrator access to your web server, check with someone who does.
CGI passes information to Python in the form of a class called FieldStorage. In your Pythonic CGI script, you must create an instance of FieldStorage in order to access the CGI data. The main attribute of class FieldStorage which you will need to know is 'getvalue'. For example, the data from an address book form might be accessed as follows:
# Import modules for CGI handling import cgi, cgitb # Create instance of FieldStorage form = cgi.FieldStorage() # Get data from field 'name' name = form.getvalue('name') # Get data from field 'address' address = form.getvalue('address') # Get data from field 'phone' phone = form.getvalue('phone') # Get data from field 'email' email = form.getvalue('email')
From here, the data has been assigned to the variables. You can thus handle the values within the program like other literals.
Every instance of class FieldStorage (e.g., 'form') has the following attributes:
form.name The name of the field, if it is specified
form.filename If an FTP transaction, the client-side filename
form.value The value of the field as a string
form.file file object from which data can be read
form.type The content type, if applicable
form.type_options The options of the 'content-type' line of the HTTP request, returned as a dictionary
form.disposition The field 'content-disposition'; None if unspecified
form.disposition_options The options for 'content-disposition'
form.headers All of the HTTP headers returned as a dictionary
So, instead of writing above
name = form.getvalue('name')
I could have written
name = form['name'].value
In addition to 'getvalue', the following methods may be used to access form data by field name:
In a case where each field name is unique, all three methods will return the same results. So the following will return the same data:
name = form.getvalue('name') name = form.getfirst('name') name = form.getlist('name')
The method 'getlist', however, will return a list of values. The others will simply return a string.
Python's calendar module is part of the standard library. It allows the output of a calendar by month or by year and also provides other, calendar-related functionality.
The calendar module itself depends on the datetime module. But we will also need datetime for our own purposes, as we will see. So we should import both of these. Also, in order to do some string splitting, we will need the re module. Let's import them all in one go.
import re, datetime, calendar
By default, the calendars begin the week with Monday (day 0), per the European convention, and ends with Sunday (day 6). If you prefer Sunday as the first day of the week, use the setfirstweekday() method to change the default to day 6 as follows:
To toggle between the two, you could pass the first day of the week as an argument using the sys module. You would then check the value with an if statement and set the setfirstweekday() method accordingly.
import sys firstday = sys.argv if firstday == "6": calendar.setfirstweekday(6)