V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Casibase - 开源AI知识库
Casibase是一套开源的基于LangChain的AI知识库,支持国内外数十种主流AI模型,支持MCP协议,所有会话数据云端存储,支持Office文件存储与向量索引,支持思维链和提示词调优模块,支持多命名空间与企业SSO单点登录。
Promoted by veotax
ft3312591
V2EX  ›  Python

求教用 requests 库写下载脚本问题

  •  
  •   ft3312591 · 2018-06-15 15:34:45 +08:00 · 1565 次点击
    这是一个创建于 2601 天前的主题,其中的信息可能已经有所发展或是发生改变。

    代码如下

    r = requests.get(url,  stream = True,timeout = 10)
    stream_size = r.headers['Content-Length'] #获取下载文件大小
    with open(name + '.mp4', 'wb') as f:
        for chunk in r.iter_content(chunk_size = 1024):
        f.write(chunk) #有时候会卡在这里,即一直在等待下一个 chunk。
    
    

    有时候下载视频,服务器不会返拒绝超时之类的错误(能返回错误倒是好解决),但就是不返回下个 chunk 数据,一直等啊等 所以脚本有时候就卡在那不动了,有办法避免这种情况吗? 获取下个 chunk 值这一段可以加上 timeout 的阈值吗?

    目前尚无回复
    关于   ·   帮助文档   ·   自助推广系统   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   974 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 25ms · UTC 20:04 · PVG 04:04 · LAX 13:04 · JFK 16:04
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.