[Pkg-privacy-commits] [txtorcon] 53/96: simplify and modernize launch_tor example
Jérémy Bobbio
lunar at moszumanska.debian.org
Sun Sep 6 18:33:39 UTC 2015
This is an automated email from the git hooks/post-receive script.
lunar pushed a commit to branch master
in repository txtorcon.
commit d4416dc087e0a401dd6322e222db06cda6d66d5d
Author: meejah <meejah at meejah.ca>
Date: Thu Feb 5 17:15:25 2015 -0700
simplify and modernize launch_tor example
---
examples/launch_tor.py | 72 ++++++++++++++++++--------------------------------
1 file changed, 25 insertions(+), 47 deletions(-)
diff --git a/examples/launch_tor.py b/examples/launch_tor.py
index 489bc6b..0c6cf7b 100755
--- a/examples/launch_tor.py
+++ b/examples/launch_tor.py
@@ -2,63 +2,41 @@
# Launch a slave Tor by first making a TorConfig object.
-import functools
-from twisted.internet import reactor
+from sys import stdout
+from twisted.internet.task import react
+from twisted.internet.defer import inlineCallbacks
import txtorcon
-def finished(answer):
- print "Answer:", answer
- print "We could now do any sort of exciting thing we wanted..."
- print "...but instead, we'll just exit."
- reactor.stop()
+ at inlineCallbacks
+def main(reactor):
+ config = txtorcon.TorConfig()
+ config.OrPort = 1234
+ config.SocksPort = 9999
+ try:
+ yield txtorcon.launch_tor(config, reactor, stdout=stdout)
+ except RuntimeError as e:
+ print "Error:", e
+ return
-def query_changed_config(answer, state):
- # now we'll ask for the ORPort back to prove it changed
- state.protocol.get_conf("ORPort").addCallback(finished)
-
-
-def state_complete(config, state):
- print "We've completely booted up a TorState to a Tor version %s at PID %d" % (state.protocol.version, state.tor_pid)
+ proto = config.protocol
+ print "Connected to Tor version", proto.version
+ state = yield txtorcon.TorState.from_protocol(proto)
+ print "This Tor has PID", state.tor_pid
print "This Tor has the following %d Circuits:" % len(state.circuits)
for c in state.circuits.values():
print c
- config.SOCKSPort = 0
- config.ORPort = 9089
- # "save" may be poorly-named API; it serializes the options to the
- # running Tor (via SETCONF calls)
- config.save().addCallback(query_changed_config, state)
-
-
-def setup_complete(config, proto):
- print "setup complete:", proto
- print "Building a TorState"
- state = txtorcon.TorState(proto.tor_protocol)
- # Pass the config object yet again, avoiding global state
- state.post_bootstrap.addCallback(functools.partial(state_complete, config))
- state.post_bootstrap.addErrback(setup_failed)
-
-
-def setup_failed(arg):
- print "SETUP FAILED", arg
- reactor.stop()
-
-
-def updates(prog, tag, summary):
- print "%d%%: %s" % (prog, summary)
-
+ print "Changing our config (SOCKSPort=9876)"
+ config.SOCKSPort = 9876
+ yield config.save()
-config = txtorcon.TorConfig()
-config.OrPort = 1234
-config.SocksPort = 9999
+ print "Querying to see it changed:"
+ socksport = yield proto.get_conf("SOCKSPort")
+ print "SOCKSPort", socksport
-# Launch tor. The config-object is passed by the closure around
-# setup_complete()
-d = txtorcon.launch_tor(config, reactor, progress_updates=updates)
-d.addCallback(functools.partial(setup_complete, config))
-d.addErrback(setup_failed)
-reactor.run()
+if __name__ == '__main__':
+ react(main)
--
Alioth's /usr/local/bin/git-commit-notice on /srv/git.debian.org/git/pkg-privacy/packages/txtorcon.git
More information about the Pkg-privacy-commits
mailing list