#!/usr/local/bin/python #-- # counter.py: Web page counter script. Use this Server-Side # Include directive: # # where: # counter is the compiled version of counter.c, with the # setuid bit. # yourfile is the name of the file where the hits will be stored. #-- import sys, string def Error ( * L ): """Displays an error message in HTML. [ if (L is a list of strings) -> +:= concatenation of elements of L stop execution ] """ print string.join ( L, "") sys.exit(1) # - - - c o u n t e r . p y - - m a i n - - - - - #-- # Retrieve the command line argument, which should be the file # name, and open the file if possible. If not, assume the # counter is zero. #-- argList=sys.argv[1:] if len(argList) != 1: Error ( "Page programming error: the #exec command must provide " "a file name argument.") fileName = argList[0] try: f = open ( fileName ) line = string.rstrip(f.readline()) try: counter = string.atoi(line) except ValueError: counter = 0 f.close() except IOError: counter = 0 #-- # Add one, rewrite the file (if possible), and output the new value. #-- counter = counter + 1 if counter == 1: suffix="" else: suffix="s" print "This page has been hit %d time%s." % (counter, suffix) try: f = open ( fileName, "w" ) f.write ( "%d\n" % counter ) except IOError: Error ( "Can't open the counter file `%s' for writing." % fileName )