CGI Webcounter not quite working...help, please

Bengt Richter bokr at oz.net
Wed Aug 6 06:13:40 EDT 2003


On Wed, 6 Aug 2003 11:01:04 +1000, Dave Harrison <dave at nullcube.com> wrote:

>> 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
         ^^^^ 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.
>> 
>> 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 '<HEAD><TITLE>My CGI-generated Page ??</TITLE></HEAD>'
>> 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 ;-) ....
>
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 '<html><head><title>My CGI-generated Page ??</title></head><body>' # ??
>
>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




More information about the Python-list mailing list