(self)
return conn
Establish a new connection via the SOCKS proxy.
Establish a new connection via the SOCKS proxy.
Establish a new connection via the SOCKS proxy .
def _new_conn(self):
        """
        Establish a new connection via the SOCKS proxy.
        """
        extra_kw = {}
        if self.source_address:
            extra_kw['source_address'] = self.source_address

        if self.socket_options:
            extra_kw['socket_options'] = self.socket_options

        try:
            conn = socks.create_connection(
                (self.host, self.port),
                proxy_type=self._socks_options['socks_version'],
                proxy_addr=self._socks_options['proxy_host'],
                proxy_port=self._socks_options['proxy_port'],
                proxy_username=self._socks_options['username'],
                proxy_password=self._socks_options['password'],
                proxy_rdns=self._socks_options['rdns'],
                timeout=self.timeout,
                **extra_kw
            )

        except SocketTimeout:
            raise ConnectTimeoutError(
                self, "Connection to %s timed out. (connect timeout=%s)" %
                (self.host, self.timeout))

        except socks.ProxyError as e:
            # This is fragile as hell, but it seems to be the only way to raise
            # useful errors here.
            if e.socket_err:
                error = e.socket_err
                if isinstance(error, SocketTimeout):
                    raise ConnectTimeoutError(
                        self,
                        "Connection to %s timed out. (connect timeout=%s)" %
                        (self.host, self.timeout)
                    )
                else:
                    raise NewConnectionError(
                        self,
                        "Failed to establish a new connection: %s" % error
                    )
            else:
                raise NewConnectionError(
                    self,
                    "Failed to establish a new connection: %s" % e
                )

        except SocketError as e:  # Defensive: PySocks should catch all these.
            raise NewConnectionError(
                self, "Failed to establish a new connection: %s" % e)

        return conn
def _new_conn ( self ) : extra_kw = { } if self . source_address : extra_kw [ 'source_address' ] = self . source_address if self . socket_options : extra_kw [ 'socket_options' ] = self . socket_options try : conn = socks . create_connection ( ( self . host , self . port ) , proxy_type = self . _socks_options [ 'socks_version' ] , proxy_addr = self . _socks_options [ 'proxy_host' ] , proxy_port = self . _socks_options [ 'proxy_port' ] , proxy_username = self . _socks_options [ 'username' ] , proxy_password = self . _socks_options [ 'password' ] , proxy_rdns = self . _socks_options [ 'rdns' ] , timeout = self . timeout , * * extra_kw ) except SocketTimeout : raise ConnectTimeoutError ( self , "Connection to %s timed out. (connect timeout=%s)" % ( self . host , self . timeout ) ) except socks . ProxyError as e : # This is fragile as hell, but it seems to be the only way to raise # useful errors here. if e . socket_err : error = e . socket_err if isinstance ( error , SocketTimeout ) : raise ConnectTimeoutError ( self , "Connection to %s timed out. (connect timeout=%s)" % ( self . host , self . timeout ) ) else : raise NewConnectionError ( self , "Failed to establish a new connection: %s" % error ) else : raise NewConnectionError ( self , "Failed to establish a new connection: %s" % e ) except SocketError as e : # Defensive: PySocks should catch all these. raise NewConnectionError ( self , "Failed to establish a new connection: %s" % e ) return conn