Download counter

Ivo Spam at
Thu Mar 4 20:07:18 CET 2004

people why does this not work??

I have tried to write a download manager (counter) for my website
called something like this: http://<server>/cgi-bin/

I do get a download popup but the download is broken. Someting is not right
and I don't know what.

Excuse my sloppy code... It is stil in test.

Code below, help appreciated.
import cgi
import os
import glob
import sys

__author__ = "Ivo Woltring"
__version__ = "00.01"

Enable download with additional features like counters, logging, no direct
downloads, etc.

# Realmap is the reference to the physic location of the root map
# of your site
RealMap  = "D:/"

def WriteError(titel,text):
 print('Content-Type: text/html\r\n\r\n')
 print """\
""" % (titel,titel,text)

# Main stuff

#GetFormulier...and check it
form = cgi.FieldStorage()
if not form:
  WriteError('Download error:', 'No download was specified...')
 if form.has_key("FileID"):
  try: int(FileID)
  except: WriteError('Download error:', 'No valid ID was entered: %s' %
 else: WriteError('Download error:', 'No valid key specified...')

from IvoNetdb import IvoNetSpufi # load My database settings

 data = IvoNetSpufi("""SELECT location,archive
                          FROM download
                         WHERE id=%s
                     """ % (FileID,)) # Will return a tuple with tuples in
except: WriteError('ERROR','SQL Error.')
# update the counter
if not IvoNetSpufi("""UPDATE download
                         SET downloadcounter = downloadcounter+1
                       WHERE id = %s
                   """ % (FileID,)):
 WriteError('ERROR','Could not update downloadcounter')

if not data:
 WriteError('ERROR','Did not find a match for ID: %s' % (FileID,))
 data = data[0] # list in list is returned so make it easy on yourself
 loc = os.path.join(RealMap+data[0],data[1])
 if not os.path.isfile(loc):
  WriteError('ERROR','File does not appear to be there anymore...')
  fhandle = open(loc, 'r')
  file    =

  print('Content-Type: application/octet-stream; name=%s' % data[1])
  #print('Content-type: application/force-download')
  print('Content-length: %s' % len(file))
  print('Content-Disposition: attachment; filename="%s"' % data[1])
  print file

More information about the Python-list mailing list