Handle HTTP errors in redirects
This commit is contained in:
parent
110a1939a7
commit
651d889f6f
@ -16,16 +16,19 @@ def downloadPage(url,headers = False):
|
|||||||
class rdrh(urllib.request.HTTPRedirectHandler):
|
class rdrh(urllib.request.HTTPRedirectHandler):
|
||||||
def redirect_request(self,req,fp,code,msg,hdrs,newurl):
|
def redirect_request(self,req,fp,code,msg,hdrs,newurl):
|
||||||
response["url"] = 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)
|
opener = urllib.request.build_opener(rdrh)
|
||||||
outHeaders = []
|
outHeaders = []
|
||||||
for header in headers:
|
for header in headers:
|
||||||
outHeaders.append((header,headers[header]))
|
outHeaders.append((header,headers[header]))
|
||||||
opener.addheaders = outHeaders
|
opener.addheaders = outHeaders
|
||||||
requestHandler = opener.open(url)
|
|
||||||
|
|
||||||
try:
|
try:
|
||||||
|
requestHandler = opener.open(url)
|
||||||
response["headers"] = requestHandler.getheaders()
|
response["headers"] = requestHandler.getheaders()
|
||||||
response["body"] = requestHandler.read()
|
response["body"] = requestHandler.read()
|
||||||
except urllib.error.HTTPError as e:
|
except urllib.error.HTTPError as e:
|
||||||
|
Loading…
Reference in New Issue
Block a user