Problem with downloading from www

N

NewFilmFan

I use Python 2.3 on Windows XP.
I wrote this program:

import httplib
conn = httplib.HTTPConnection("www.x.net")
conn.request("GET", "/x/y.jpg")
r1 = conn.getresponse()
print r1.status, r1.reason
data = r1.read()
datei = open('test.jpg','w')
datei.write(data)
datei.close()

It is almost a copy of the manual. Now I can establish the connection and
receive the data. (The first response from the server is 200, OK.) The jpg
file is 198 K. But it is not a valid jpeg file. When I download with my
browser, the file has the length 197K and it is valid, of course. Is this
a problem of writing text or binary data to a file? Can anybody help me?

TIA,

nff
 
F

Fredrik Lundh

NewFilmFan said:
I wrote this program:

import httplib
conn = httplib.HTTPConnection("www.x.net")
conn.request("GET", "/x/y.jpg")
r1 = conn.getresponse()
print r1.status, r1.reason
data = r1.read()
datei = open('test.jpg','w')
datei.write(data)
datei.close()

if all you want to do is to download files from the web, urllib is a lot more
convenient:

urllib.urlretrieve("http://www.x.net/x.y.jpg", "test.jpg")
It is almost a copy of the manual. Now I can establish the connection and
receive the data. (The first response from the server is 200, OK.) The jpg
file is 198 K. But it is not a valid jpeg file. When I download with my
browser, the file has the length 197K and it is valid, of course. Is this
a problem of writing text or binary data to a file?

changing the open() call to

datei = open('test.jpg','wb')

should fix this.

see the library documentation for details.

</F>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,175
Latest member
Vinay Kumar_ Nevatia
Top