return to first page linux journal archive
keywordscontents
#!/usr/local/bin/python
#
# CGI count - Uses a gdbm database
#

from string import atoi, zfill
import gdbm
import cgi


DIGITS = 4


hex_bytes = [
     [ "0xff,0xff,", "0xff,0xff,", "0xff,0xff,", "0xff,0xff,",
"0xff,0xff,", "0xff,0xff,", "0xff,0xff,", "0xff,0xff,",
"0xff,0xff,", "0xff,0xff," ],
     [ "0x01,0xc0,",     "0x01,0xc0,", "0x01,0xc0,",
"0x01,0xc0,", "0x01,0xc0,", "0x01,0xc0,", "0x01,0xc0,",
"0x01,0xc0,", "0x01,0xc0,", "0x01,0xc0," ],
     [ "0xfd,0xdf,",     "0xfd,0xdf,", "0xfd,0xdf,",
"0xfd,0xdf,", "0xfd,0xdf,", "0xfd,0xdf,", "0xfd,0xdf,",
"0xfd,0xdf,", "0xfd,0xdf,", "0xfd,0xdf," ],
     [ "0x3d,0xde,",     "0x7d,0xde,", "0x1d,0xde,",
"0x3d,0xdc,", "0xfd,0xdd,", "0x7d,0xd0,", "0xfd,0xd1,",
"0x0d,0xd0,", "0x1d,0xdc,", "0x1d,0xde," ],
     [ "0xdd,0xdd,",     "0x1d,0xde,", "0x0d,0xdc,",
"0xdd,0xd8,", "0xfd,0xdc,", "0x3d,0xd8,", "0x7d,0xde,",
"0x0d,0xd8,", "0xcd,0xd9,", "0xcd,0xd9," ],
     [ "0xcd,0xd9,",     "0x7d,0xde,", "0xe5,0xd8,",
"0xed,0xd9,", "0x7d,0xdc,", "0xbd,0xdf,", "0x3d,0xdf,",
"0xf5,0xdb,", "0xe5,0xd3,", "0xed,0xd9," ],
     [ "0xed,0xdb,",     "0x7d,0xde,", "0xf5,0xd9,",
"0xfd,0xd9,", "0xbd,0xdc,", "0x1d,0xdf,", "0x9d,0xdf,",
"0xfd,0xdb,", "0xe5,0xd3,", "0xe5,0xd3," ],
     [ "0xe5,0xd3,",     "0x7d,0xde,", "0xfd,0xd9,",
"0xfd,0xdd,", "0xdd,0xdc,", "0x1d,0xdc,", "0xcd,0xdf,",
"0xfd,0xdd,", "0xc5,0xd3,", "0xe5,0xd3," ],
     [ "0xe5,0xd3,",     "0x7d,0xde,", "0xfd,0xd9,",
"0x7d,0xde,", "0xdd,0xdc,", "0x7d,0xd8,", "0x2d,0xdc,",
"0xfd,0xdd,", "0x8d,0xd9,", "0xe5,0xd3," ],
     [ "0xe5,0xd3,",     "0x7d,0xde,", "0xfd,0xdd,",
"0x3d,0xdc,", "0xed,0xdc,", "0xfd,0xd1,", "0xc5,0xd9,",
"0xfd,0xdd,", "0x1d,0xde,", "0xc5,0xd3," ],
     [ "0xe5,0xd3,",     "0x7d,0xde,", "0xfd,0xde,",
"0xfd,0xd8,", "0xf5,0xdc,", "0xfd,0xd3,", "0xe5,0xd1,",
"0xfd,0xde,", "0x3d,0xdc,", "0xcd,0xd3," ],
     [ "0xe5,0xd3,",     "0x7d,0xde,", "0xfd,0xde,",
"0xfd,0xd1,", "0xf5,0xdc,", "0xfd,0xd3,", "0xe5,0xd3,",
"0xfd,0xde,", "0xcd,0xd8,", "0x1d,0xd8," ],
     [ "0xe5,0xd3,",     "0x7d,0xde,", "0x7d,0xdf,",
"0xfd,0xd3,", "0x05,0xd0,", "0xfd,0xd7,", "0xe5,0xd3,",
"0xfd,0xde,", "0xe5,0xd1,", "0xfd,0xd9," ],
     [ "0xed,0xdb,",     "0x7d,0xde,", "0xbd,0xdf,",
"0xfd,0xd3,", "0xfd,0xdc,", "0xfd,0xd7,", "0xe5,0xd3,",
"0x7d,0xdf,", "0xe5,0xd3,", "0xfd,0xdd," ],
     [ "0xcd,0xd9,",     "0x7d,0xde,", "0xdd,0xd7,",
"0xfd,0xdb,", "0xfd,0xdc,", "0xfd,0xdb,", "0xcd,0xdb,",
"0x7d,0xdf,", "0xe5,0xd3,", "0x7d,0xde," ],
     [ "0xdd,0xdd,",     "0x7d,0xde,", "0x0d,0xd0,",
"0xcd,0xd9,", "0xfd,0xdc,", "0xcd,0xdd,", "0xcd,0xd9,",
"0x7d,0xdf,", "0xcd,0xd9,", "0x3d,0xdf," ],
     [ "0x3d,0xde,",     "0x1d,0xd8,", "0x05,0xd8,",
"0x0d,0xde,", "0xfd,0xdc,", "0x0d,0xde,", "0x3d,0xdc,",
"0xbd,0xdf,", "0x1d,0xdc,", "0xc5,0xdf," ],
     [ "0xfd,0xdf,",     "0xfd,0xdf,", "0xfd,0xdf,",
"0xfd,0xdf,", "0xfd,0xdf,", "0xfd,0xdf,", "0xfd,0xdf,",
"0xfd,0xdf,", "0xfd,0xdf,", "0xfd,0xdf," ],
     [ "0x01,0xc0,",     "0x01,0xc0,", "0x01,0xc0,",
"0x01,0xc0,", "0x01,0xc0,", "0x01,0xc0,", "0x01,0xc0,",
"0x01,0xc0,", "0x01,0xc0,", "0x01,0xc0," ],
     [ "0xff,0xff,",     "0xff,0xff,", "0xff,0xff,",
"0xff,0xff,", "0xff,0xff,", "0xff,0xff,", "0xff,0xff,",
"0xff,0xff,", "0xff,0xff,", "0xff,0xff," ]
]

def print_header() :
    ############
     print "Content-type: image/x-bitmap"
     print
     print "#define counter_width", DIGITS * 16
     print "#define counter_height 20"
     print "static char counter_bits[] = {"


def print_footer() :
    ############
     print "0x00 } ;"
     print


def print_digits_values( s ) :
    ###################
     i = 0
     while i < 20 :
          for d in s :
               print hex_bytes[ i ][ atoi( d ) ],
          print
          i = i + 1


def inc_counter( s ) :
    ###########
     val = atoi( s ) + 1
     return zfill( str( val ), DIGITS )


def get_put_counter( url ) :
    ###############
     db = gdbm.open( "counters.gdbm", "w", 0644 )
     if db.has_key( url ) :
          s = db[ url ]
     else :
          s = zfill( '0', DIGITS )
     s = inc_counter( s )
     db[ url ] = s
     return s


def CGImain() :
    #######
     list = cgi.SvFormContentDict()
     if list.has_key( "url" ) :
          url = list[ "url" ]
          counter = get_put_counter( url )
          print_header()
          print_digits_values( counter )
          print_footer()


CGImain()