downloading jpeg using python--extra bytes?

Bigpond huy at nsw.bigpond.net.au
Tue Apr 25 08:13:22 EDT 2000


Try using the urllib module,

it handles all this low level stuff for you.

one line can do all that you have specified below, and it works...


Gabriel Berger <gberger at channel1.com> wrote in message
news:sf5001f0cqv98 at corp.supernews.com...
> Hello,
>
> I wrote a python script to download jpegs from web sites.
> (I realize that it's possible to do this using my browser, but
> as an exercise, I'd like to be able to do it directly.)
>
> The script works, with one exception: the jpeg image has
> extra bytes in it that corrupt the file.  I'm not sure exactly
> how the bytes are dispersed, but they're not all at the
> beginning or at the end.
>
> I would imagine that I need to modify my http request, but
> I don't know how to do so.  Any help would be appreciated.
>
> Here is the script:
>
> import httplib
> h = httplib.HTTP('www.something.com')
> h.putrequest('GET','/directory/picture.jpg')
> h.putheader('Accept','image/jpeg')
> h.endheaders()
> errcode,errmsg,headers = h.getreply()
> f=h.getfile()
> data=f.read()
> f.close()
> g = open("myjpeg.jpg","w")
> g.write(data)
> g.close()
>
> Thanks in advance,
>
> Gabriel Berger
>
>





More information about the Python-list mailing list