我是Python初学者。如果下载视频文件的进程超过500秒,我希望能够设置超时时间。
import urllib
try:
urllib.urlretrieve ("http://www.videoURL.mp4", "filename.mp4")
except Exception as e:
print("error")
我该如何修改代码才能实现这一点?
我是Python初学者。如果下载视频文件的进程超过500秒,我希望能够设置超时时间。
import urllib
try:
urllib.urlretrieve ("http://www.videoURL.mp4", "filename.mp4")
except Exception as e:
print("error")
我该如何修改代码才能实现这一点?
requests
,这样您就可以流式传输结果并轻松检查超时:import requests
# Make the actual request, set the timeout for no data to 10 seconds and enable streaming responses so we don't have to keep the large files in memory
request = requests.get('http://www.videoURL.mp4', timeout=10, stream=True)
# Open the output file and make sure we write in binary mode
with open('filename.mp4', 'wb') as fh:
# Walk through the request response in chunks of 1024 * 1024 bytes, so 1MiB
for chunk in request.iter_content(1024 * 1024):
# Write the chunk to the file
fh.write(chunk)
# Optionally we can check here if the download is taking too long
尽管urlretrieve
没有这个功能,但您仍然可以为所有新的套接字对象设置默认超时时间(以秒为单位)。
import socket
import urllib
socket.setdefaulttimeout(15)
try:
urllib.urlretrieve ("http://www.videoURL.mp4", "filename.mp4")
except Exception as e:
print("error")
urlretrieve
没有这个选项。但是你可以很容易地使用urlopen
并将结果写入文件来执行你的示例,如下所示:
request = urllib.urlopen("http://www.videoURL.mp4", timeout=500)
with open("filename.mp4", 'wb') as f:
try:
f.write(request.read())
except:
print("error")
如果你正在使用Python 3,那么你应该使用urllib库。但是如果你正在使用Python 2,你应该使用urllib2库。
urllib.request.urlopen()
函数等同于 urllib2.urlopen()
,而 urllib.urlopen()
已被删除。在 3.6 版本中,正确的调用方式是 urllib.request.urlopen()
。我不知道是否有 Python 版本可用 urllib.urlopen()
,因此我不会编辑答案。 - Andreas Haferburg