19

Python beginner here. I want to be able to timeout my download of a video file if the process takes longer than 500 seconds.

import urllib
try:
   urllib.urlretrieve ("http://www.videoURL.mp4", "filename.mp4")
except Exception as e:
   print("error")

How do I amend my code to make that happen?

Ned Hulton
  • 477
  • 3
  • 12
  • 27

3 Answers3

16

Better way is to use requests so you can stream the results and easily check for timeouts:

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
Wolph
  • 78,177
  • 11
  • 137
  • 148
11

Although urlretrieve does not have this feature, you can still set the default timeout (in seconds) for all new socket objects.

import socket
import urllib    

socket.setdefaulttimeout(15)

try:
   urllib.urlretrieve ("http://www.videoURL.mp4", "filename.mp4")
except Exception as e:
   print("error")
Vojtech Semecky
  • 131
  • 1
  • 6
4

urlretrieve does not have that option. But you can easily perform your example with the help of urlopen and writing the result in a file, like so:

request = urllib.urlopen("http://www.videoURL.mp4", timeout=500)
with open("filename.mp4", 'wb') as f:
    try:
        f.write(request.read())
    except:
        print("error")

That's if you are using Python 3. If you are using Python 2, you should rather use urllib2.

Djizeus
  • 4,161
  • 1
  • 24
  • 42
  • 1
    urlopen can be easy, but for a large file. request.read() can be slow and take forever, you should consider adding a timeout around that function, probably using singal package. – fanchyna Feb 20 '16 at 16:48
  • Not only can it be slow, it could fail completely. For example, suppose the file is 10GB in size, and won't fit in memory. – LarsH Jun 12 '19 at 21:02
  • `Also note that the urllib.request.urlopen() function in Python 3 is equivalent to urllib2.urlopen() and that urllib.urlopen() has been removed.` The correct call in 3.6 is `urllib.request.urlopen()`. I don't know if maybe there is a Python version where `urllib.urlopen()` actually works, so I won't edit the answer. – Andreas Haferburg Nov 14 '19 at 21:04