python - ProtocolError IncompleteRead using requests -


i got wierd error when tried download images using requests quite brief code follows,

import requests import stringio  r = requests.get(image_url, stream=true) if r.status_code == 200:     r.raw.decode_content = true     data = stringio.stringio(r.raw.data)     # other code deal data 

then error,

protocolerror: ('connection broken: incompleteread(15060 bytes read, 55977 more expected)', incompleteread(15060 bytes read, 55977 more expected)) 

i googled similar problems, , try force requests using http/1.0 protocol this,

import httplib httplib.httpconnection._http_vsn = 10 httplib.httpconnection._http_vsn_str = 'http/1.0' 

however, server returns me 403 status code.

by way, what's more confusing protocolerror not happens every time sometimes.

any appreciated!

since using stream=true should iterate on response , save file in chunks:

with open('pic1.jpg', 'wb') handle:     response = requests.get(image_url, stream=true)      if response.ok:         block in response.iter_content(1024):             if not block:                 break             handle.write(block) 

note save actual file, can modified use stringio:

with stringio() handle:     response = requests.get(image_url, stream=true)      if response.ok:         block in response.iter_content(1024):             if not block:                 break             handle.write(str(block)) 

Comments

Popular posts from this blog

mysql - Dreamhost PyCharm Django Python 3 Launching a Site -

java - Sending SMS with SMSLib and Web Services -

java - How to resolve The method toString() in the type Object is not applicable for the arguments (InputStream) -