[Tutor] HTTP file download

Jason Massey jason.massey at gmail.com
Wed Mar 14 12:44:26 CET 2007


I've tested this on my Apache server setup, it succesfully downloads the gif
file and the text file and saves them.  Even though I used the 'wb' flag
(write binary) for the text file it turned out okay (the difference in 'w'
and 'wb' seemed to be that 'wb' stripped off some of my blank lines).  One
thing to keep in mind is that you if the file you're downloading has spaces
in the filename you'll need to replace the space with '%20' so it'll
download correctly

import urllib

files = ['game_config.txt','apache_pb.gif']

for f in files:
    mysock = urllib.urlopen("http://localhost/%s" % f)
    fileToSave = mysock.read()
    oFile = open(r"c:\python24\%s" % f,'wb')
    oFile.write(fileToSave)
    oFile.close

On 3/14/07, Ronaldo <ronaldo at netf2r.com> wrote:
>
>   Hi, Jean
>
>   I've alredy tried this, but it seems that this kind of code just works
> when "file" in the url (http://www.somesite.com/file) is an html file.
> The thing is that "file" in this case is a text file. For example: if I
> try to download the file using a web browser, it asks me for a directory
> to save the file.
>   Do you have any other idea?
>
>   Thanks.
>
> Jean-Philippe Durand wrote:
> > Hello Ronaldo,
> > Try this :
> > import urllib
> >
> > mysock = urllib.urlopen("http://www.somesite.com/file")
> >
> > htmlSource = mysock.read()
> > mysock.close()
> >
> > print htmlSource
> > Regards.
> > Jean-Philippe DURAND
> >
> >
> > 2007/3/13, ronaldo at netf2r.com <mailto:ronaldo at netf2r.com>
> > <ronaldo at netf2r.com <mailto:ronaldo at netf2r.com>>:
> >
> >     Hello all,
> >
> >     How can I download a file using HTTP?
> >     For example:
> >     There is a file at: http://www.somesite.com/file. I need to get
> >     this file using HTTP from a python script.
> >     I'm not sure but I think httplib could be used to do that.
> >     Can anyone confirm that? or Can anyone suggest me something else?
> >
> >     Thank you?
> >
> >     --
> >     Ronaldo Z. Afonso
> >     Phone: 55+11+82619082
> >     www.netf2r.com <http://www.netf2r.com>
> >     _______________________________________________
> >     Tutor maillist  -  Tutor at python.org <mailto:Tutor at python.org>
> >     http://mail.python.org/mailman/listinfo/tutor
> >     <http://mail.python.org/mailman/listinfo/tutor>
> >
> >
>
>
> _______________________________________________
> Tutor maillist  -  Tutor at python.org
> http://mail.python.org/mailman/listinfo/tutor
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.python.org/pipermail/tutor/attachments/20070314/c04036d8/attachment.htm 


More information about the Tutor mailing list