Image Download with PycURL gets me broken Images
I'm struggling with pycurl. These are my headers:
headers.append('User-Agent: Mozilla/5.0 (Windows NT 6.1; rv:5.0) Gecko/20100101 Firefox/5.0')
head开发者_C百科ers.append('Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8')
headers.append('Accept-Language: de-de,de;q=0.8,en-us;q=0.5,en;q=0.3')
headers.append('Accept-Encoding: gzip, deflate')
headers.append('Accept-Charset: UTF-8,*')
headers.append('Connection: Keep-Alive')
Original:
What I get:
As you can see, the one I get with pycurl is broken. If I compare them with a text-compare-tool, it tells me they are the same. (There was a difference in line-endings where the original had only LF and the broken had CRLF, but I changed that and now I have identical images, still broken)
The host where I'm downloading from, is not the reason. I tried to do the same from the dropbox and a local apache. Both didn't work.
This is how I save the image:
self.buffer = StringIO.StringIO()
# other curl options like ssl, cookies, followlocation and GET Request URL Setup to the Image: http://dl.dropbox.com/u/25733986/test.jpg
self.curl.setopt(pycurl.WRITEFUNCTION, self.buffer.write)
# -> curl.perform()
f = open("temp/resources/%s" % (filename,), 'w')
f.write(self.buffer.getvalue())
f.close()
I would be happy if anyone has some suggestions on this, so I can find my error.
Okay, now where I have finally meditated over this by posting this question I got the solution.
I needed to open the file in binary mode.
f = open("temp/resources/%s" % (filename,), 'wb')
I hope this Question helps someone else, sometime, anyhow.
Thanks stackoverflow for making me meditate on this. :)
精彩评论