28.4. Python Examples
This section demonstrates a few examples of how Python can be used for CGI. The examples show how you can leverage Python's capabilities and add-on modules to deliver dynamic document content.
28.4.1. Date and Time Handling
Data and time handling functions are useful in everyday system administration scripts all the way up to an actual calendar application. Python offers the time module, the datetime module, and the calendar module. The time module provides functionality for accessing and formatting time/date strings by calling platform C functions that perform the desired task. The datetime module, new in Python 2.3, supplies classes for manipulating dates and times either simply or in a more complex way. Typically, datetime functions are considered to be improvements over the older time module functionality. The calendar module completes the date and time handling functions, giving Python everything it needs for simple and complex date and time operations.
Example 5: A Simple CalendarSource — Python-Example05.py #!/usr/bin/python import os, sys import cgi import time import calendar from datetime import datetime #Set up date variables date = datetime.now().date() today= date.strftime("%m %Y") thisyear = date.strftime("%Y") thismonth = date.strftime("%m") thisday = date.strftime("%d") #Write out the document header def docheader(month, year): ''' month is a string with the month number year is a string with the year ''' t = time.strptime(month, '%m') ... |
Get Web Standards Programmer's Reference: HTML, CSS, JavaScript®, Perl, Python®, and PHP now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.