global urllib import urllib.request global html import html global downloadPage def downloadPage(window,downloadId,url,headers = False): global browserWindowsLock global browserWindows if not headers: headers = {} response = { "url": url, "body": "", "headers": [] } class rdrh(urllib.request.HTTPRedirectHandler): def redirect_request(self,req,fp,code,msg,hdrs,newurl): response["url"] = 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.addheaders = headers try: requestHandler = opener.open(url) response["headers"] = requestHandler.getheaders() response["body"] = requestHandler.read() except urllib.error.HTTPError as e: response["body"] = e.read() except Exception as e: response["body"] = html.escape(str(e)).encode("utf-8") browserWindowsLock.acquire() if not window in browserWindows: browserWindowsLock.release() return window.cTaskLock.acquire() if window.cDownloadId != downloadId: window.cTaskLock.release() browserWindowsLock.release() return window.cTasks.append((window.cHandleResponse,[response],{})) window.cTaskLock.release() browserWindowsLock.release()