python - StreamConsumedError() exception raised by "for chunk in response.iter_content(1024)" -
i'm not sure what's best way ask question,
but i'm writing script download content internet using request.get(), working fine, reason getting streamconsumederror() being raised, , i'm not sure why.
traceback (most recent call last): file "./pythondl-ver_0.0.4.py", line 90, in <module> chunk in response.iter_content(1024): file "/library/frameworks/python.framework/versions/2.7/lib/python2.7/site-packages/requests/models.py", line 766, in iter_content raise streamconsumederror() requests.exceptions.streamconsumederror if need anymore information can add in. thanks.
while true: count = count + 1 print 'testing ' + str(count) + '\n' print url url_2 = url.format(count = count) print '////' + str(url_2) + ' loop ' + str(count) + "////\n" print str(url_2) + ' testing url \n' filename = posixpath.basename(url_2) print str(filename) + ' testing filename \n' response = requests.get(url_2, stream = true) responsestring = str(response) print str(responsestring) + 'testing res2 \n' if responsestring == '<response [404]>': print '......no more requests......\n' break elif responsestring == '<response [200]>': print '......successful request....\n' else: break print responsestring while responsestring == '<response [200]>': print 'testing while loop: ' + str(responsestring) open(filename, 'wb') fp: chunk in response.iter_content(1024): fp.write(chunk) count += 1 print str(count) + ' = counting value' here's loop things stopped working.
while responsestring == '<response [200]>': print 'testing while loop: ' + str(responsestring) open(filename, 'wb') fp: chunk in response.iter_content(1024): fp.write(chunk) this loop never terminates, once responsestring <response [200]> stays forever, because nothing changes that.
the thing prevents loop go forever can read streaming response several times. when loop tries again response throws error.
the simplest fix replace while if
if responsestring == '<response [200]>': open(filename, 'wb') fp: chunk in response.iter_content(1024): fp.write(chunk)
Comments
Post a Comment