473,503 Members | 1,687 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

CGI Webcounter not quite working...help, please

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

Jul 18 '05 #1
3 1538
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

Jul 18 '05 #2
> 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>'

Jul 18 '05 #3
On Wed, 6 Aug 2003 11:01:04 +1000, Dave Harrison <da**@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 statementprint '<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
Jul 18 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
7520
by: Phill Long | last post by:
this is the the code, now here is the final result.... I get one combo box and one tex box come up, but they are empty... DAMN!!! Any ideas on what Im doing wrong please.. Thanks Again <?php...
5
1937
by: J. W. McCall | last post by:
Sorry if this is OT, but here's my question: I wrote a simple python script to increment a counter in a text file, and I wanted this script to be accessed whenever an HTML file is accessed. The...
13
4296
by: Joner | last post by:
Hello, I'm having trouble with a little programme of mine where I connect to an access database. It seems to connect fine, and disconnect fine, but then after it won't reconnect, I get the error...
3
930
by: Mike | last post by:
Hey guys I am pulling my hair out on this problem!!!!! Any help or ideas or comments on how to make this work I would be grateful! I have been working on this for the past 4 days and nothing I do...
21
1938
by: salad | last post by:
Thanks for reading this post and wasting your time. I was thinking about writing about the PCDatasheet vs The Pussyfarts war. The pussyfarts, as near as I can tell, have little to offer this...
1
9584
by: David Van D | last post by:
Hi there, A few weeks until I begin my journey towards a degree in Computer Science at Canterbury University in New Zealand, Anyway the course tutors are going to be teaching us JAVA wth bluej...
22
2145
by: KitKat | last post by:
I need to get this to go to each folders: Cam 1, Cam 2, Cam 4, Cam 6, Cam 7, and Cam 8. Well it does that but it also needs to change the file name to the same folder where the file is being...
1
1068
by: Chris | last post by:
ASP.NET 2.0 was working fine, then I installed XP SP2 an it stopped working. Unfortunately I do need SP2 so going back to SP1 is not an option. I am quite certain that it was working for a while...
0
7086
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
7280
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7332
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6991
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7462
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5014
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4673
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3167
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.