CGI script to count downloads

Greg Ewing greg at cosc.canterbury.ac.nz
Tue Apr 3 01:25:34 EDT 2001


I'm trying to write a script that will keep a count
of the number of times a file is downloaded. So far,
the script just takes the name of the required file,
opens the file, and passes on its contents.

It works fine, except for one annoying thing:
When Netscape pops up the box requesting a file
name for saving, the default filename seems to be
constructed in a screwy way from parts of the
URL. For instance, if the URL used to retrieve the
file is

  download.py?file=download/akaroa2.6.1doc.tar.gz

then the default filename that Netscape gives me
is

  akaroa2.6.1doc.tar.py

So, my question is, can I put anything in the
headers of the response to give the browser a
hint as to what name the file should be saved
under?

Or is there a better way of going about this
whole counting business?

This is the script I'm using:

  #!/usr/local/cosc/bin/python
  print "Content-Type: compressed/gzip"
  print
  import cgi, sys
  form = cgi.FieldStorage()
  path = form["file"].value
  f = open(path)
  sys.stdout.write(f.read())
  f.close()

-- 
Greg Ewing, Computer Science Dept, University of Canterbury,	  
Christchurch, New Zealand
To get my email address, please visit my web page:	  
http://www.cosc.canterbury.ac.nz/~greg



More information about the Python-list mailing list