CGI Webcounter not quite working...help, please

Discussion in 'Python' started by J. W. McCall, Aug 5, 2003.

  1. J. W. McCall

    J. W. McCall Guest

    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
    J. W. McCall, Aug 5, 2003
    #1
    1. Advertising

  2. J. W. McCall

    Karl Scalet Guest

    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
    Karl Scalet, Aug 5, 2003
    #2
    1. Advertising

  3. > 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>'
    Dave Harrison, Aug 6, 2003
    #3
  4. On Wed, 6 Aug 2003 11:01:04 +1000, Dave Harrison <> 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
    Bengt Richter, Aug 6, 2003
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    6
    Views:
    478
    Andy Dingley
    Dec 27, 2003
  2. J. W. McCall

    Can't get dumb webcounter working

    J. W. McCall, Aug 3, 2003, in forum: Python
    Replies:
    5
    Views:
    261
    Jane Doe
    Aug 4, 2003
  3. DanWeaver
    Replies:
    0
    Views:
    268
    DanWeaver
    Apr 13, 2008
  4. Replies:
    2
    Views:
    313
    sk_usenet
    Apr 30, 2008
  5. Gary Mayor
    Replies:
    3
    Views:
    266
    Rob Swindells
    Mar 4, 2004
Loading...

Share This Page