help debug cgi counter?

Pete Shinners pshinners at mediaone.net
Mon Sep 11 02:51:43 EDT 2000


ok, awhile back my very first python program was a CGI webcounter.
the good news, it's working. the bad news, everynow and then "bad
things" happen. i assume this is because of two instances running
at the same time (???)

unfortunately, i can find no obvious solution. occasionally the
counter will show "1" when it can't read the counter file properly,
and even less frequently it will reset the counter file to an
empty file.

---------------------myfirstpython.py------------------

#!/usr/bin/python

def runcounter(filename):
        try:
                file = open(filename, 'r')
                count = int(file.readline())
                file.close()
                count = count+1
                file = open(filename, 'w')
                file.write(str(count))
                file.close()
        except:
                count = 1

        return count


if __name__ == '__main__':
        #print 'Content-type: text/html\n\n'
        print runcounter('/home/shinners/.cgiinfo/counter')

--------------------------------------------------------

thanks all. i haven't much experience with CGI, but this should
be a pretty simple cases. the "best" examples i could find were
PERL CGI counters, but i don't know PERL and couldn't quite follow
the code. (they were surprisingly long, but fiddled with a
"lock file", which i can't think of the best usage for something
like that)

ok, a little wiser for the wear, i now know the "except:" line
wasn't the best coding habit. but i had to start somewhere :]

note, this is called from an SSL include, so i didn't need
the "text/html" mime header info.







More information about the Python-list mailing list