给定 wget 命令的 Python 等价物 [英] Python equivalent of a given wget command
问题描述
我正在尝试创建一个与此 wget 命令执行相同操作的 Python 函数:
I'm trying to create a Python function that does the same thing as this wget command:
wget -c --read-timeout=5 --tries=0 "$URL"
-c
- 如果下载中断,则从上次中断的地方继续.
-c
- Continue from where you left off if the download is interrupted.
--read-timeout=5
- 如果超过 5 秒没有新数据传入,则放弃并重试.给定 -c
这意味着它会从它停止的地方再次尝试.
--read-timeout=5
- If there is no new data coming in for over 5 seconds, give up and try again. Given -c
this mean it will try again from where it left off.
--tries=0
- 永远重试.
这三个参数串联使用导致下载不会失败.
Those three arguments used in tandem results in a download that cannot fail.
我想在我的 Python 脚本中复制这些功能,但我不知道从哪里开始......
推荐答案
urllib.请求 应该工作.只需在一段时间(未完成)循环中设置它,检查本地文件是否已存在,是否确实发送了带有 RANGE 标头的 GET,指定您下载本地文件的距离.请务必使用 read() 附加到本地文件,直到发生错误.
urllib.request should work. Just set it up in a while(not done) loop, check if a localfile already exists, if it does send a GET with a RANGE header, specifying how far you got in downloading the localfile. Be sure to use read() to append to the localfile until an error occurs.
这也可能是 Python urllib2 恢复下载的副本网络重新连接时不起作用
这篇关于给定 wget 命令的 Python 等价物的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!