downloading a file

Richard van de Stadt stadt at cs.utwente.nl
Wed Jan 5 06:29:02 EST 2000


Anders M Eriksson wrote:

With a name like that you should be working for a certain
Telecommunications company :-)

> 
> Hello!
> 
> I need a script which will log the number of times a specifik file has
> been downloaded.
> 
> I have created a script that will do the logging but I don't know how
> to perform the downloading.  I want it to behave like when you
> download a file thru the web browser. The user just select the link
> and gets the standard downloading question by the web browser.
> 
> I have tried to use httplib but I don't know what kind of file to
> 'Accept'
> 
> // Anders

What about this:

import urllib, string
url = 'http://the_url_of_the_file_to_download'
data = urllib.urlopen(url).read()
#find the format of the file (usually the stuff after the rightmost dot):
suffix = url[string.rfind(url, '.') + 1:]
#Let the browser know what's coming:
if suffix == 'ps': apptype = 'postscript'
elif suffix == 'pdf': apptype = 'pdf'
elif suffix == 'zip': apptype = 'x-zip-compressed'
elif suffix == 'doc': apptype = 'msword'
elif suffix == 'gz': apptype = 'x-gzip'
elif suffix == 'Z': apptype = 'x-compress'
else apptype = 'unknown'
print 'Content-type: application/' + apptype + '\n'
#now simply print the data to the browser:
print data


Richard.



More information about the Python-list mailing list