From 651d889f6fda4a6032e2ab642b72775e05fd3aeb Mon Sep 17 00:00:00 2001 From: Fierelier Date: Sat, 20 Mar 2021 17:57:46 +0100 Subject: [PATCH] Handle HTTP errors in redirects --- addons/0.pageDownloader.py | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/addons/0.pageDownloader.py b/addons/0.pageDownloader.py index 3a097cf..083c475 100644 --- a/addons/0.pageDownloader.py +++ b/addons/0.pageDownloader.py @@ -16,16 +16,19 @@ def downloadPage(url,headers = False): class rdrh(urllib.request.HTTPRedirectHandler): def redirect_request(self,req,fp,code,msg,hdrs,newurl): response["url"] = newurl - return urllib.request.HTTPRedirectHandler.redirect_request(self, req, fp, code, msg, hdrs, newurl) + try: + return urllib.request.HTTPRedirectHandler.redirect_request(self, req, fp, code, msg, hdrs, newurl) + except urllib.error.HTTPError as e: + return e opener = urllib.request.build_opener(rdrh) outHeaders = [] for header in headers: outHeaders.append((header,headers[header])) opener.addheaders = outHeaders - requestHandler = opener.open(url) try: + requestHandler = opener.open(url) response["headers"] = requestHandler.getheaders() response["body"] = requestHandler.read() except urllib.error.HTTPError as e: