Cgi Counter

Rikard Bosnjakovic rikard at strakt.com
Mon Oct 15 02:00:53 EDT 2001


Neal Irwin wrote:
 
> Does anyone have sample code for a web-page hit
> counter. I was curious what one would look like in
> Python. I have made one in Perl and would like to
> compare them.

You can use some ugly hack like this:

#!/usr/bin/env python
import os.path

FILE = "/tmp/counter"

if not os.path.exists(FILE):
    c = 0
else:
    c = int(open(FILE).readline())

print c

# increase the counter
c = c + 1
open(FILE, "w").write(str(c))



Gives:

>>> ## working on region in file /tmp/python-3601Dum...
0
>>> ## working on region in file /tmp/python-3601Q4s...
1
>>> ## working on region in file /tmp/python-3601dCz...
2




Cheers,
Rikard



More information about the Python-list mailing list