import urllib.request,urllib.error
import socket
try:
response = urllib.request.urlopen(url, timeout=5).read().decode('utf-8')
except urllib.error.HTTPError as error:
logging.error('Data not retrieved because %s\nURL: %s', error, url)
except urllib.error.URLError as error:
if isinstance(error.reason, socket.timeout):
logging.error('socket timed out - URL %s', url)
else:
logging.error('some other error happened')
except socket.timeout as error:
logging.error('socket timed out - URL %s', url)
else:
logging.info('Access successful.')