Download counter

Ivo Spam at ivonet.nl
Thu Mar 4 14:07:18 EST 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/download.py?FileID=1

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.
===
#!d:/python/python.exe
#e:/python23/python.exe
import cgi
import os
import glob
import sys

__author__ = "Ivo Woltring"
__version__ = "00.01"

__doc__="""
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:/IvoNet.nl"
#===============================================================

def WriteError(titel,text):
 print('Content-Type: text/html\r\n\r\n')
 print """\
<HTML><HEADER><TITLE>%s</TITLE></HEADER>
<BODY>
<H2>%s</H2>
%s
</BODY></HTML>
""" % (titel,titel,text)
 sys.exit(1)


#===============
# Main stuff
#===============

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

from IvoNetdb import IvoNetSpufi # load My database settings

try:
 data = IvoNetSpufi("""SELECT location,archive
                          FROM download
                         WHERE id=%s
                     """ % (FileID,)) # Will return a tuple with tuples in
it.
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,))
else:
 #WriteError('data',data)
 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...')
 else:
  fhandle = open(loc, 'r')
  file    = fhandle.read()
  fhandle.close()

  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
  print file
  #sys.stdout.write(file)
==





More information about the Python-list mailing list