Add extra exceptions to get_web_page

This commit is contained in:
Ken Roberts 2015-02-18 08:13:36 -08:00
parent 80055b7ec0
commit 2b63b5d3ad

View File

@ -29,6 +29,7 @@ import locale
import os import os
import platform import platform
import re import re
import socket
import time import time
from shutil import which from shutil import which
from subprocess import Popen, PIPE from subprocess import Popen, PIPE
@ -394,26 +395,33 @@ def get_web_page(url, header=None, update_openlp=False):
req.add_header('User-Agent', user_agent) req.add_header('User-Agent', user_agent)
if header: if header:
req.add_header(header[0], header[1]) req.add_header(header[0], header[1])
page = None
log.debug('Downloading URL = %s' % url) log.debug('Downloading URL = %s' % url)
retries = 1 retries = 0
while True: while retries <= CONNECTION_RETRIES:
retries += 1
time.sleep(0.1)
try: try:
page = urllib.request.urlopen(req, timeout=CONNECTION_TIMEOUT) page = urllib.request.urlopen(req, timeout=CONNECTION_TIMEOUT)
log.debug('Downloaded URL = %s' % page.geturl()) log.debug('Downloaded page {}'.format(page.geturl()))
except (urllib.error.URLError, ConnectionError): except urllib.error.URLError as err:
if retries > CONNECTION_RETRIES: log.exception('URLError on {}'.format(url))
log.exception('The web page could not be downloaded') log.exception('URLError: {}'.format(err.reason))
raise page = None
else: except socket.timeout:
retries += 1 log.exception('Socket timeout: {}'.format(url))
time.sleep(0.1) page = None
continue except ConnectionRefusedError:
break log.exception('ConnectionRefused: {}'.format(url))
if not page: page = None
return None break
except ConnectionError:
log.exception('Connection error: {}'.format(url))
page = None
if update_openlp: if update_openlp:
Registry().get('application').process_events() Registry().get('application').process_events()
if not page:
log.exception('{} could not be downloaded'.format(url))
return None
log.debug(page) log.debug(page)
return page return page