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
Post a Comment