Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to handle urllib's timeout in Python 3?

First off, my problem is quite similar to this one. I would like a timeout of urllib.urlopen() to generate an exception that I can handle.

Doesn't this fall under URLError?

try:     response = urllib.request.urlopen(url, timeout=10).read().decode('utf-8') except (HTTPError, URLError) as error:     logging.error(         'Data of %s not retrieved because %s\nURL: %s', name, error, url) else:     logging.info('Access successful.') 

The error message:

resp = urllib.request.urlopen(req, timeout=10).read().decode('utf-8')
File "/usr/lib/python3.2/urllib/request.py", line 138, in urlopen
return opener.open(url, data, timeout)
File "/usr/lib/python3.2/urllib/request.py", line 369, in open
response = self._open(req, data)
File "/usr/lib/python3.2/urllib/request.py", line 387, in _open
'_open', req)
File "/usr/lib/python3.2/urllib/request.py", line 347, in _call_chain
result = func(*args)
File "/usr/lib/python3.2/urllib/request.py", line 1156, in http_open
return self.do_open(http.client.HTTPConnection, req)
File "/usr/lib/python3.2/urllib/request.py", line 1141, in do_open
r = h.getresponse()
File "/usr/lib/python3.2/http/client.py", line 1046, in getresponse
response.begin()
File "/usr/lib/python3.2/http/client.py", line 346, in begin
version, status, reason = self._read_status()
File "/usr/lib/python3.2/http/client.py", line 308, in _read_status
line = str(self.fp.readline(_MAXLINE + 1), "iso-8859-1")
File "/usr/lib/python3.2/socket.py", line 276, in readinto
return self._sock.recv_into(b)
socket.timeout: timed out

There was a major change from in Python 3 when they re-organised the urllib and urllib2 modules into urllib. Is it possible that there was a change then that causes this?

like image 205
nindalf Avatar asked Jan 06 '12 19:01

nindalf


People also ask

Which is better Urllib or requests?

2) urllib provides the urlencode method which is used for the generation of GET query strings, urllib2 doesn't have such a function. This is one of the reasons why urllib is often used along with urllib2. Requests - Requests' is a simple, easy-to-use HTTP library written in Python.

How do I open a URL in Python 3?

url= 'https://www.javatpoint.com/python-tutorial' # Open the URL using open() function of module. webbrowser. open_new_tab(url)

Is Urllib built in Python 3?

The urllib module in Python 3 allows you access websites via your program. This opens up as many doors for your programs as the internet opens up for you. urllib in Python 3 is slightly different than urllib2 in Python 2, but they are mostly the same.


1 Answers

The exception is timeout from socket, so

from socket import timeout try:     response = urllib.request.urlopen(url, timeout=10).read().decode('utf-8') except (HTTPError, URLError) as error:     logging.error('Data of %s not retrieved because %s\nURL: %s', name, error, url) except timeout:     logging.error('socket timed out - URL %s', url) else:     logging.info('Access successful.') 

should catch the new exception.

like image 163
danodonovan Avatar answered Sep 22 '22 19:09

danodonovan