#!/usr/bin/env python

##
## Launch a slave Tor by first making a TorConfig object.
##

import sys
import types

from twisted.internet import reactor
from zope.interface import implements

import txtorcon

def state_complete(state):
    print "We've completely booted up a TorState to a Tor version %s at PID %d" % (state.protocol.version, state.tor_pid)

    print "This Tor has the following %d Circuits:" % len(state.circuits)
    for c in state.circuits.values():
        print c

    print "We could now do any sort of exciting thing we wanted..."
    print "...but instead, we'll just exit."
    reactor.stop()

def setup_complete(proto):
    print "setup complete:",proto
    print "Building a TorState"
    state = txtorcon.TorState(proto.tor_protocol)
    state.post_bootstrap.addCallback(state_complete)
    state.post_bootstrap.addErrback(setup_failed)

def setup_failed(arg):
    print "SETUP FAILED",arg
    reactor.stop()

config = txtorcon.TorConfig()
config.OrPort = 1234
config.SocksPort = 9999

def updates(prog, tag, summary):
    print "%d%%: %s" % (prog, summary)

d = txtorcon.launch_tor(config, reactor, progress_updates=updates)
d.addCallback(setup_complete)
d.addErrback(setup_failed)
reactor.run()

@@ -207,413 +207,970 @@
 def 
-state_complete(state):%0A    print %22We've completely booted up a TorState to a Tor version %25s at PID %25d%22 %25 (state.protocol.version, state.tor_pid)%0A%0A    print %22This Tor has the following %25d Circuits:%22 %25 len(state.circuits)%0A    for c in state.circuits.values():%0A        print c%0A%0A    print %22We could now do any sort of exciting thing we wanted...%22%0A    print %22...but instead, we'll just exit.%22%0A    reactor.stop(
+finished(answer):%0A    print %22Answer:%22, answer%0A    print %22We could now do any sort of exciting thing we wanted...%22%0A    print %22...but instead, we'll just exit.%22%0A    reactor.stop()%0A%0Adef query_changed_config(answer, state):%0A    # now we'll ask for the ORPort back to prove it changed%0A    state.protocol.get_conf(%22ORPort%22).addCallback(finished)%0A%0Adef state_complete(state):%0A    print %22We've completely booted up a TorState to a Tor version %25s at PID %25d%22 %25 (state.protocol.version, state.tor_pid)%0A%0A    print %22This Tor has the following %25d Circuits:%22 %25 len(state.circuits)%0A    for c in state.circuits.values():%0A        print c%0A%0A    # we're using the global to demonstrate that the config we passed%0A    # to launch_tor can be used after the Tor has been started%0A    global config%0A    config.ORPort = 9090%0A    # %22save%22 may be poorly-named API; it serializes the options to the%0A    # running Tor (via SETCONF calls)%0A    config.save().addCallback(query_changed_config, state
 )%0A%0Ad

make example a little longer to show use of TorConfig after launch