#! /usr/bin/python2 # use C:\Python22\python.exe in windows # Help at the end of the file import cgi import fcntl import time lockfile = "/tmp/.lock" print "Content-Type: text/plain" # HTML is following print "Expires: 0" print "Cache-Control: no cache" print # blank line, end of headers ## Open a file-descriptor for the lock file. lockH = open(lockfile,"w") ## Try to aquire an exclusive lock on the lock file. try: fcntl.flock(lockH , fcntl.LOCK_EX) # | fcntl.LOCK_NB) except IOError,e: sys.exit("Lockfile %s already locked - an instance is probably already running" % lockfile) form = cgi.FieldStorage() if not (form.has_key("CounterName")): print "0" # "0 (no key)" else: FileName="CounterData/" + form["CounterName"].value # Get number try: f = open(FileName,"r") except IOError: print "0" # "0 (no file)" else: Hits=f.read() Hits = str(int(Hits)+1) print Hits # "(from file)" f.close() f = open(FileName,"w") f.write(Hits) f.close() ## Release the lock again fcntl.flock(lockH, fcntl.LOCK_UN) ############################################################################### # This is a simple _text based_ web counter writen in python # It works in unix and Windows (with python installed and some modifications) # # Make sure the 1st line reflects where you python interpreter is located # Place this script in your cgi-bin directory and make sure it's executable "chmod 711" # Make a subdirectory cgi-bin/CounterData # You need to create each counter you want with "echo 1 >cgi-bin/CounterData/PageCounter" # You could for instance name them the same than your html pages, leaving out the ".html" # # Embed the counters within your pages with the following html code: # # With the Counter class defined in your .css file as # .Counter { border-style:none; width:6em; height:1em; overflow:visible; } # You can use as many counters as you want # # Written 2004 Guillaume Dargaud - Free use and modification ###############################################################################