Cgi Counter

limodou chatme at 263.net
Mon Oct 15 04:48:52 EDT 2001


class counter:
    filename=""
    count=0
    def __init__(self, filename):
        self.filename=filename
        try:
            f=open(filename, "r")
        except IOError:
            print "Can't open file", filename
        else:
            s=f.readline()
            self.count=int(s)
            f.close()
    def inc(self):
        try:
            f=open(self.filename, "w")
        except IOError:
            print "Can't open file", filename
        else:
            self.count+=1
            f.write(str(self.count))
            f.close()
            
import Cookie,os

#judge Cookie,get user's IP from the cookie
c=Cookie.Cookie()
try:
    c.load(os.environ["HTTP_COOKIE"])
    userip=c["userip"]
except:
    userip=""

userhost=os.environ["REMOTE_ADDR"]

#set cookie
if(userip==""):
    c["userip"]=userhost
    c["userip"]["expires"]=3600
    c["userip"]["path"]="/"
    print c["userip"]

print "Content-type: text/html"
print

a=counter("count.txt")
if(userip==""): a.inc()
print str(a.count)+"<br>"



More information about the Python-list mailing list