From 1f37dc83f6287a840d2e10c85ffbb7bf2ddff0af Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Fri, 22 Sep 2017 22:04:28 +0200 Subject: Put a timeout for all url fetch in imports --- chimere/utils.py | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) (limited to 'chimere/utils.py') diff --git a/chimere/utils.py b/chimere/utils.py index ba0f205..ffa5789 100644 --- a/chimere/utils.py +++ b/chimere/utils.py @@ -50,6 +50,8 @@ from django.utils.translation import ugettext_lazy as _ from chimere import get_version +TIMEOUT = 20 + def unicode_normalize(string): return ''.join( @@ -202,7 +204,7 @@ class ImportManager(object): url = source if extra_url: url += extra_url - remotehandle = urllib.request.urlopen(url) + remotehandle = urllib.request.urlopen(url, timeout=TIMEOUT) source = io.BytesIO(remotehandle.read()) remotehandle.close() except ValueError: @@ -1195,7 +1197,7 @@ class HtmlXsltManager(ImportManager): self.marker_cls = Marker try: main_page = urllib.request.urlopen(self.importer_instance.source, - timeout=20) + timeout=TIMEOUT) assert main_page.getcode() == 200 except (urllib.error.URLError, AssertionError): return (0, 0, _("Source page is unreachable.")) @@ -1243,7 +1245,8 @@ class HtmlXsltManager(ImportManager): not c_item['link'].startswith('https://'): c_item['link'] = base_url + c_item['link'] try: - child_page = urllib.request.urlopen(c_item['link']) + child_page = urllib.request.urlopen(c_item['link'], + timeout=TIMEOUT) assert child_page.getcode() == 200 except (urllib.error.URLError, AssertionError): # don't stop the export for a bad link -- cgit v1.2.3