#!/usr/bin/python counterFileName = 'ajax-sample-counter.txt' # Read the current counter from the file and add 1. counterFile = open(counterFileName) s = counterFile.read().strip() try: counter = int(s) + 1 except Exception, e: counter = 1 counterFile.close() # Write the new current counter to the file. counterFile = open(counterFileName, 'w') print >>counterFile, counter counterFile.close() print "Content-type: text/plain\r\n\r\n", print counter