CGI script to count downloads
Remco Gerlich
scarblac at pino.selwerd.nl
Tue Apr 3 05:24:44 EDT 2001
Greg Ewing <greg at cosc.canterbury.ac.nz> wrote in comp.lang.python:
> 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?
<clpm> This is a CGI question! </clpm> ;)
According to the CGI faq at http://www.htmlhelp.com/faq/cgifaq.html ,
chapter 4 question 22, you can use the URL
download.py/filename?options=anyextraoptions
And the filename should be in "PATH_INFO" (env var, I suppose).
Weeding through some other CGI scripts that Google dug up...
Content-type: compressed/gzip; name=%s
might work.
Another CGI script I found uses the header
Content-disposition: inline; filename=%s
And notes that it probably only works on Netscape.
Seen enough Perl for today now...
> Or is there a better way of going about this
> whole counting business?
Maybe analyze logs?
> 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()
That needs a whole lot of extra checking on the filename of course...
--
Remco Gerlich
More information about the Python-list
mailing list