Best Practice to Set Timeout for Python urllib.request.urlretrieve() – Python Web Crawler Tutorial

By | September 1, 2019

In most python application, if you plan to download some files, urllib.request.urlretrieve() will be your choice. The simplest reason is there is a callback function in it, we can show downloading progress bar in that callback function.

downloading progress bar example

Syntax of urllib.request.urlretrieve()

urllib.request.urlretrieve(url, filename=None, reporthook=None, data=None)

However, we can not set timeout like urllib.request.urlopen() or In this tutorial, we will introduce a simple way to set timeout for it.

From source code, we can find urllib.request.urlretrieve() will create a socket to connect and download file. We can set a global timeout for socket.

import socket
socket.setdefaulttimeout(time = 60) # 60 seconds

This timeout value will be urllib.request.urlretrieve() timeout.