downloading jpeg using python--extra bytes?

Gabriel Berger gberger at channel1.com
Mon Apr 10 21:26:19 EDT 2000


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