CGI Webcounter not quite working...help, please

J

J. W. McCall

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))
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.

It's on my Freeshell shell account. It's a NetBSD system, I believe.

Any advice, ideas?

Thanks,

J. W. McCall
 
K

Karl Scalet

J. W. McCall said:
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
 
D

Dave Harrison

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).

you might want to try embedding the page itself in the code (ugly, but if you're just starting out with python maybe the way to go).
#!/usr/pkg/bin/python

print "Content-Type: text/html\n\n"
print "\n\n"

this part is unnecessary, you already have the two \n's from the statement above it.
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))
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.

hmmm ... ok well here's how I would do something similar, we dont want to just guess if a file is there, we want it to be there and error else (or so I think, it is a counting script, and a couting script without a file cant really work ;-)

Now while Im sure this isnt the best way to do it (tho I dont think its all that bad either ;-) ....

#!/usr/bin/env python

import string

print 'Content-Type: text/html\n\n'
print '<html><body>'

count = '0'

try:
count = open('count.txt').read()
count = string.strip(count)
num = string.atoi(count)
print "You are the "+str(num+1)+"th visitor to this page"
except IOError:
print "No valid file"
except ValueError:
print "No valid count value"

print '</body></html>'
 
B

Bengt Richter

you might want to try embedding the page itself in the code (ugly, but if you're just starting out with python maybe the way to go).
^^^^ is this where the ISP has python? When the CGI script runs, does it run
as you (i.e., under your uid and permissions) or as user "nobody" or "www" or such?
Ask the sysadmin what the policy is. Or they should have a FAQ someplace about their stuff.
this part is unnecessary, you already have the two \n's from the statement above it.


hmmm ... ok well here's how I would do something similar, we dont want to just guess if a file is there, we want it to be there and error else (or so I think, it is a counting script, and a couting script without a file cant really work ;-)

Now while Im sure this isnt the best way to do it (tho I dont think its all that bad either ;-) ....
A couple of nits and (untested) suggestions:
#!/usr/bin/env python

import string
import sys # for exc_info in case weird exception
print 'Content-Type: text/html\n\n'
print 'Content-Type: text/html\n' # you get one from the print statement
print '<html><body>'
print ' said:
count = '0'

try:
count = open('count.txt').read()
count = string.strip(count)
num = string.atoi(count)
num = count and int(count) or 0
print "You are the "+str(num+1)+"th visitor to this page"
except IOError:
print "No valid file"
except ValueError:
print "No valid count value"
# ugh, just noticed the tabs ...
except Exception, e:
# print the name and message of any standard exception remaining
print '%s: %s' % (e.__class__, e)
except:
print 'Nonstandard Exception %r: %r' % sys.exc_info()[:2]
print '</body></html>'

Or you can let this do the exception stuff:
http://www.python.org/doc/current/lib/module-cgitb.html
See also
http://www.python.org/doc/current/lib/module-cgi.html
for good reading.

Regards,
Bengt Richter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,906
Latest member
SkinfixSkintag

Latest Threads

Top