#!/usr/bin/python # # cgicount.py - A simple hit count CGI script # # # Author: Alex Shindich (http://www.shindich.com/) # import sys import cgi import string import XBMNumbers16x20 def PrintError (errorMessage): """This function prints out an error message and exits the program""" print 'Content Type: text/html\n' print '%s\n' % errorMessage sys.exit () def IncNumber(fileName): """This function reads the number from a source file, increments it and writes the new number value back into the file""" # Read the number from a file and increment try: f = open (fileName, 'r') line = f.readline () number = string.atoi (line) + 1 except: number = 1 try: # Save the new number in a file f = open (fileName, 'w') f.write (str(number)) f.flush () except: PrintError ("Failed to write into file '%s'" % fileName) return number def GetFileName (): """This function obtains the file name from cgi parameters""" try: # Get the cgi parameters parameters = cgi.SvFormContentDict() # We are looking for the file name here if parameters.has_key ("filename"): return parameters ["filename"] else: PrintError ('No file specified') except: PrintError ('Unknown error while getting the file name parameter') def main() : # Redirect the error output into the standard output sys.stderr = sys.stdout # Get the file name fileName = GetFileName () # Increment the number in the file newNumber = IncNumber (fileName) # Increment the number in a file and print it out as an XBM file print XBMNumbers16x20.XBMNumbers16x20 (newNumber, 4, inverse=0) if __name__ == '__main__': main ()