Add redirect support

This commit is contained in:
Fierelier 2021-03-20 17:23:38 +01:00
parent 09e9adc3e7
commit ebb20248cb
2 changed files with 14 additions and 3 deletions

View File

@ -140,6 +140,7 @@ class browserWindow(QMainWindow):
self.update()
start = time.time()
response = downloadPage(self.cUrlBar.text(),{"User-Agent": self.cUserAgent})
self.cUrlBar.setText(response["url"])
end = time.time()
print("Downloading page: " +str(end - start))

View File

@ -12,10 +12,20 @@ def downloadPage(url,headers = False):
"body": None,
"headers": []
}
request = urllib.request.Request(url,headers=headers)
requestHandler = None
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)
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 = urllib.request.urlopen(request)
response["headers"] = requestHandler.getheaders()
response["body"] = requestHandler.read()
except urllib.error.HTTPError as e: