1

I am struggling with try and except with python 3

I could catch the HTTPError(Bad Request) and URLError(no host/route name found)

However I couldn't catch the timeout error yet.

 while True:
        try:
            f = urllib.request.urlretrieve(url,csvname)
        except urllib.error.HTTPError as e: #
            print(str(chl) + " Error:" + str(e))


            continue
        except urllib.error.URLError as e: 
            continue
        except socket.timeout as e:
            #I can't catch time out error here
            continue

it returns this.

How can I prevent the script from stopping with timeout error?

Traceback (most recent call last):
  File "wisdom2.py", line 84, in <module>
    f = urllib.request.urlretrieve(url,csvname)
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/urllib/request.py", line 186, in urlretrieve
    with contextlib.closing(urlopen(url, data)) as fp:
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/urllib/request.py", line 161, in urlopen
    return opener.open(url, data, timeout)
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/urllib/request.py", line 464, in open
    response = self._open(req, data)
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/urllib/request.py", line 482, in _open
    '_open', req)
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/urllib/request.py", line 442, in _call_chain
    result = func(*args)
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/urllib/request.py", line 1211, in http_open
    return self.do_open(http.client.HTTPConnection, req)
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/urllib/request.py", line 1186, in do_open
    r = h.getresponse()
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/http/client.py", line 1227, in getresponse
    response.begin()
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/http/client.py", line 386, in begin
    version, status, reason = self._read_status()
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/http/client.py", line 348, in _read_status
    line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
  File "/Users/whitebear/.pyenv/versions/3.4.6/lib/python3.4/socket.py", line 378, in readinto
    return self._sock.recv_into(b)
TimeoutError: [Errno 60] Operation timed out

     except socket.timeout as e:
NameError: name 'socket' is not defined
cs95
  • 379,657
  • 97
  • 704
  • 746
whitebear
  • 11,200
  • 24
  • 114
  • 237

2 Answers2

6

On python 3.x, TimeoutError is a builtin exception class. You can catch it with

except TimeoutError:
    ...

On python 2.x, you have two options:

  1. Catch urllib.socket.timeout exception

  2. Catch requests.Timeout exception

cs95
  • 379,657
  • 97
  • 704
  • 746
0

you need to import socket before you use it:

from urllib import socket

or specify that the socket you're talking about comes from urllib, ie:

except urllib.socket.timeout as e:
Stael
  • 2,619
  • 15
  • 19
  • ImportError: cannot import name 'socket' I have tried upper one and bump into this error. – whitebear Aug 01 '17 at 16:31
  • are you trying to run code from here: https://stackoverflow.com/questions/2712524/handling-urllib2s-timeout-python – Stael Aug 01 '17 at 16:32
  • maybe they've changed it in python 3, but the comments suggest that you don't need the socket, the URLerror catches both. – Stael Aug 01 '17 at 16:33