(reactor, control_endpoint=None, password_function=None)
Creates a :class:`txtorcon.Tor` instance by connecting to an
    already-running tor's control port. For example, a common default
    tor uses is UNIXClientEndpoint(reactor, '/var/run/tor/control') or
    TCP4ClientEndpoint(reactor, 'localhost', 9051)

    If only password authentication is available in the tor we connect
    to, the ``password_function`` is called (if supplied) to retrieve
    a valid password. This function can return a Deferred.

    For example::

        import txtorcon
        from twisted.internet.task import react
        from twisted.internet.defer import inlineCallbacks

        @inlineCallbacks
        def main(reactor):
            tor = yield txtorcon.connect(
                TCP4ClientEndpoint(reactor, "localhost", 9051)
            )
            state = yield tor.create_state()
            for circuit in state.circuits:
                print(circuit)

    :param control_endpoint: None, an IStreamClientEndpoint to connect
        to, or a Sequence of IStreamClientEndpoint instances to connect
        to. If None, a list of defaults are tried.

    :param password_function:
        See :class:`txtorcon.TorControlProtocol`

    :return:
        a Deferred that fires with a :class:`txtorcon.Tor` instance
Creates a :class:`txtorcon.Tor` instance by connecting to an
    already-running tor's control port. For example, a common default
    tor uses is UNIXClientEndpoint(reactor, '/var/run/tor/control') or
    TCP4ClientEndpoint(reactor, 'localhost', 9051)
Creates a : class : txtorcon . Tor instance by connecting to an already - running tor s control port . For example a common default tor uses is UNIXClientEndpoint ( reactor / var / run / tor / control ) or TCP4ClientEndpoint ( reactor localhost 9051 )
def connect(reactor, control_endpoint=None, password_function=None):
    """
    Creates a :class:`txtorcon.Tor` instance by connecting to an
    already-running tor's control port. For example, a common default
    tor uses is UNIXClientEndpoint(reactor, '/var/run/tor/control') or
    TCP4ClientEndpoint(reactor, 'localhost', 9051)

    If only password authentication is available in the tor we connect
    to, the ``password_function`` is called (if supplied) to retrieve
    a valid password. This function can return a Deferred.

    For example::

        import txtorcon
        from twisted.internet.task import react
        from twisted.internet.defer import inlineCallbacks

        @inlineCallbacks
        def main(reactor):
            tor = yield txtorcon.connect(
                TCP4ClientEndpoint(reactor, "localhost", 9051)
            )
            state = yield tor.create_state()
            for circuit in state.circuits:
                print(circuit)

    :param control_endpoint: None, an IStreamClientEndpoint to connect
        to, or a Sequence of IStreamClientEndpoint instances to connect
        to. If None, a list of defaults are tried.

    :param password_function:
        See :class:`txtorcon.TorControlProtocol`

    :return:
        a Deferred that fires with a :class:`txtorcon.Tor` instance
    """

    @inlineCallbacks
    def try_endpoint(control_ep):
        assert IStreamClientEndpoint.providedBy(control_ep)
        proto = yield control_ep.connect(
            TorProtocolFactory(
                password_function=password_function
            )
        )
        config = yield TorConfig.from_protocol(proto)
        tor = Tor(reactor, proto, _tor_config=config)
        returnValue(tor)

    if control_endpoint is None:
        to_try = [
            UNIXClientEndpoint(reactor, '/var/run/tor/control'),
            TCP4ClientEndpoint(reactor, '127.0.0.1', 9051),
            TCP4ClientEndpoint(reactor, '127.0.0.1', 9151),
        ]
    elif IStreamClientEndpoint.providedBy(control_endpoint):
        to_try = [control_endpoint]
    elif isinstance(control_endpoint, Sequence):
        to_try = control_endpoint
        for ep in control_endpoint:
            if not IStreamClientEndpoint.providedBy(ep):
                raise ValueError(
                    "For control_endpoint=, '{}' must provide"
                    " IStreamClientEndpoint".format(ep)
                )
    else:
        raise ValueError(
            "For control_endpoint=, '{}' must provide"
            " IStreamClientEndpoint".format(control_endpoint)
        )

    errors = []
    for idx, ep in enumerate(to_try):
        try:
            tor = yield try_endpoint(ep)
            txtorlog.msg("Connected via '{}'".format(ep))
            returnValue(tor)
        except Exception as e:
            errors.append(e)
    if len(errors) == 1:
        raise errors[0]
    raise RuntimeError(
        'Failed to connect to: {}'.format(
            ', '.join(
                '{}: {}'.format(ep, err) for ep, err in zip(to_try, errors)
            )
        )
    )
def connect ( reactor , control_endpoint = None , password_function = None ) : @ inlineCallbacks def try_endpoint ( control_ep ) : assert IStreamClientEndpoint . providedBy ( control_ep ) proto = yield control_ep . connect ( TorProtocolFactory ( password_function = password_function ) ) config = yield TorConfig . from_protocol ( proto ) tor = Tor ( reactor , proto , _tor_config = config ) returnValue ( tor ) if control_endpoint is None : to_try = [ UNIXClientEndpoint ( reactor , '/var/run/tor/control' ) , TCP4ClientEndpoint ( reactor , '127.0.0.1' , 9051 ) , TCP4ClientEndpoint ( reactor , '127.0.0.1' , 9151 ) , ] elif IStreamClientEndpoint . providedBy ( control_endpoint ) : to_try = [ control_endpoint ] elif isinstance ( control_endpoint , Sequence ) : to_try = control_endpoint for ep in control_endpoint : if not IStreamClientEndpoint . providedBy ( ep ) : raise ValueError ( "For control_endpoint=, '{}' must provide" " IStreamClientEndpoint" . format ( ep ) ) else : raise ValueError ( "For control_endpoint=, '{}' must provide" " IStreamClientEndpoint" . format ( control_endpoint ) ) errors = [ ] for idx , ep in enumerate ( to_try ) : try : tor = yield try_endpoint ( ep ) txtorlog . msg ( "Connected via '{}'" . format ( ep ) ) returnValue ( tor ) except Exception as e : errors . append ( e ) if len ( errors ) == 1 : raise errors [ 0 ] raise RuntimeError ( 'Failed to connect to: {}' . format ( ', ' . join ( '{}: {}' . format ( ep , err ) for ep , err in zip ( to_try , errors ) ) ) )