Search code examples
pythonpython-3.xtimeouturllib

Change or create a timeout with urllib request


I have this code and I was wondering if there was any way to add a timeout delay:

req = Request(url, headers={'User-Agent': 'Mozilla/5.0'})
WD = urlopen(req).read()

Solution

  • The urlopen() function has a timeout method inbuilt:

    urllib.request.urlopen(url, data=None, [timeout, ]*, cafile=None, capath=None, cadefault=False, context=None)

    The optional timeout parameter specifies a timeout in seconds for blocking operations like the connection attempt (if not specified, the global default timeout setting will be used). This actually only works for HTTP, HTTPS and FTP connections.

    So in your code:

    time = 50
    WD = urlopen(req, timeout=time).read()
    

    You can only change the request your side (i.e. client side), with the argument above. Server side may also send a timeout, but there is nothing that can be done to change that.