Handle HTTP errors in redirects

This commit is contained in:
Fierelier 2021-03-20 17:57:46 +01:00
parent 110a1939a7
commit 651d889f6f

View File

@ -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: