Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > CGI Webcounter not quite working...help, please

Reply
Thread Tools

CGI Webcounter not quite working...help, please

 
 
J. W. McCall
Guest
Posts: n/a
 
      08-05-2003
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

 
Reply With Quote
 
 
 
 
Karl Scalet
Guest
Posts: n/a
 
      08-05-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

 
Reply With Quote
 
 
 
 
Dave Harrison
Guest
Posts: n/a
 
      08-06-2003
> 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>'

 
Reply With Quote
 
Bengt Richter
Guest
Posts: n/a
 
      08-06-2003
On Wed, 6 Aug 2003 11:01:04 +1000, Dave Harrison <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
wireless range not quite enough -keevill- Wireless Networking 2 09-18-2005 09:57 PM
connecting to as400 via java servlet..not quite right Drew Java 1 02-17-2005 03:41 PM
Don't quite understand how to create a default property value, please help gwellman ASP .Net Building Controls 3 12-04-2003 07:35 PM
Anyone want a higher performance not-quite-so BigInteger pete kirkham Java 5 08-13-2003 10:32 PM
Can't get dumb webcounter working J. W. McCall Python 5 08-04-2003 01:59 PM



Advertisments