【问题标题】:StreamConsumedError() exception raised by "for chunk in response.iter_content(1024)"“for chunk in response.iter_content(1024)”引发的 StreamConsumedError() 异常
【发布时间】:2025-12-06 07:20:04
【问题描述】:

我不完全确定问这个问题的最佳方式是什么,

但我正在编写一个脚本来使用 request.get() 从 Internet 下载一些内容,一切正常,但由于某种原因,现在引发了 StreamConsumedError(),我不确定为什么。

Traceback (most recent call last):
File "./pythonDL-ver_0.0.4.py", line 90, in <module>
for 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

如果您需要更多信息,我可以添加。谢谢。

while True:
    count = count + 1
    print 'testing ' + str(count) + '\n'
    print url
    url_2 = url.format(count = count)
    print '////' + str(url_2) + ' for 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)
        with open(filename, 'wb') as fp:
            for chunk in response.iter_content(1024):
                fp.write(chunk)

    count += 1
    print str(count) + ' = counting value'

这是事情停止工作的循环。

【问题讨论】:

  • 示例代码肯定会有所帮助
  • 我已经添加了开始出错的循环,希望它有意义。
  • 您正试图重复循环遍历同一响应的iter_content
  • 哦..我傻了,我没注意到,现在可以了,谢谢

标签: python python-2.7 exception python-requests


【解决方案1】:
    while responseString == '<Response [200]>':
        print 'testing while loop: ' + str(responseString)
        with open(filename, 'wb') as fp:
            for chunk in response.iter_content(1024):
                fp.write(chunk)

这个循环永远不会终止,一旦 responseString&lt;Response [200]&gt; 它就会永远保持下去,因为没有任何改变。

唯一可以防止循环永远持续下去的是,您可以多次读取流式响应。所以当循环再次尝试响应时会抛出错误。

最简单的解决方法是将while 替换为if

    if responseString == '<Response [200]>':
        with open(filename, 'wb') as fp:
            for chunk in response.iter_content(1024):
                fp.write(chunk)

【讨论】:

  • 啊,好东西,谢谢 Nazarii。也谢谢你的解释。
最近更新 更多