CGI Webcounter not quite working...help, please

Karl Scalet news at yebu.de
Tue Aug 5 13:03:16 EDT 2003


J. W. McCall schrieb:
> Sorry again if this is OT; I'm not sure if this is a python problem or 
> just a CGI problem, but I couldn't find a decent CGI NG.  Let me know if 
> there's somewhere else I should be posting.
> 
> I got this webcounter to be called directly (I still can't get it to be 
> called from an HTML file with #exec or #include or anything).
> 
> Now the problem is that the part of the script that updates the count 
> file doesn't work.  It's like it doesn't even execute.
> 
> Here's the script; don't worry, it's short and simple (I'm a Python 
> beginner)
> 
> #!/usr/pkg/bin/python
> 
> print "Content-Type: text/html\n\n"
> print "\n\n"
> 
> import os
> import string
> 
> print "<HTML>"
> print "<BODY>"
> filenames = os.listdir(os.curdir)
> 
> if "count.txt" in filenames:
>    input = open('count.txt', 'r')
>    i = string.atoi(input.read(1))

I would do:
   i = int(input.readline())
immediately followed by:
   input.close()

Otherwise you would just read one byte, which exhosts
after 256 accesses.
This, however, is probably not related to your problem.
Also, the file must initially contain a valid integer.

> else:
>    i = 0
>    print "File doesnt exist<BR>"
> 
> i = i + 1
> print "This page has been accessed " + `i` + " times.<BR>"
> print "</BODY>"
> print "</HTML>"
> 
> #it doesn't seem to execute this at all
> output = open('count.txt', 'w')
> output.write(`i`)
> output.close()
> 
> Do you see any obvious problems with this?  It works fine when I call it 
> from the command line.

I would guess you do have read permissions on the directory
containing the file but not write permissions. As said, just
a guess. (user "you" might be different if running from command
line than running in a webserver!)

Karl





More information about the Python-list mailing list