By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,587 Members | 1,677 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,587 IT Pros & Developers. It's quick & easy.

Problem with downloading from www

P: n/a
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
----== Posted via Newsfeeds.Com - Unlimited-Uncensored-Secure Usenet News==----
http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
----= East and West-Coast Server Farms - Total Privacy via Encryption =----
Jul 18 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
"NewFilmFan" <gg@yahoo.com> wrote:
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>

Jul 18 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.