def _url_retrieve(self, url, outfd, reporthook, binary): #Like the one in urllib. Unlike urllib.retrieve url_retrieve #can be interrupted. KeyboardInterrupt exception is raised when #interrupted. count = 0 blockSize = 1024 * 8 parsed_url = urlparse(url) host = parsed_url.netloc try: proxy = proxygsettings.get_proxy_settings() if proxy and proxy.get('https'): connection = HTTPSConnection(proxy.get('https'), timeout=15) connection.set_tunnel(host) else: connection = HTTPSConnection(host, timeout=15) headers = { "Accept-Encoding": "identity", "Host": host, "User-Agent": "Python/3" } connection.request("GET", parsed_url.path, headers=headers) urlobj = connection.getresponse() assert urlobj.getcode() == 200 totalSize = int(urlobj.info()['content-length']) while not self._is_aborted(): data = urlobj.read(blockSize) count += 1 if not data: break if not binary: data = data.decode("utf-8") outfd.write(data) ui_thread_do(reporthook, count, blockSize, totalSize) except Exception as e: raise e
def wrap(self, content): self._LOGGING_ and self.logger.debug( f"[{self.name}] wrap: {len(content)} bytes") packedSotp = self.packSotp(content) if self.proxy: proxy_ip, proxy_port = self.proxy.split(":") if self.ssl: conn = HTTPSConnection(self.hostname, self.port, timeout=self.timeout, context=_create_unverified_context()) else: conn = HTTPConnection(proxy_ip, proxy_port, self.timeout) conn.set_tunnel(self.hostname, self.port) else: if self.ssl: conn = HTTPSConnection(self.hostname, self.port, timeout=self.timeout, context=_create_unverified_context()) else: conn = HTTPConnection(self.hostname, self.port, self.timeout) data_response, code_response = self.dispatchByMethod(conn, packedSotp) conn.close() self.inbox.put(self.messageToWrapper((data_response, code_response)))
def get_connection(self, hostname, **kwargs) -> HTTPSConnection: hostname = hostname.lower() if hostname in self.hostname_to_connection: return self.hostname_to_connection[hostname] else: conn = HTTPSConnection(self.hostname, self.port, **kwargs) conn.set_tunnel(hostname, 443) self.hostname_to_connection[hostname] = conn return conn
def __init__(self, parent, site_customization): InterfaceAction.__init__(self, parent, site_customization) https_proxy = get_proxies(debug=False).get('https', None) if https_proxy: https_address = ':'.join(https_proxy.split(':')[:-1]) https_port = int(https_proxy.split(':')[-1]) goodreads_conn = HTTPSConnection(https_address, https_port) goodreads_conn.set_tunnel('www.goodreads.com', 443) amazon_conn = HTTPSConnection(https_address, https_port) amazon_conn.set_tunnel('www.amazon.com', 443) else: goodreads_conn = HTTPSConnection('www.goodreads.com') amazon_conn = HTTPSConnection('www.amazon.com') self._connections = {'goodreads': goodreads_conn, 'amazon': amazon_conn} self.menu = QMenu(self.gui)