Add redirect support
This commit is contained in:
parent
09e9adc3e7
commit
ebb20248cb
@ -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))
|
||||
|
||||
|
@ -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:
|
||||
|
Loading…
Reference in New Issue
Block a user