Skip to content

Commit

Permalink
move to a listing file
Browse files Browse the repository at this point in the history
  • Loading branch information
meejah committed Feb 27, 2018
1 parent 504137d commit 3a8a4ae
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 27 deletions.
28 changes: 1 addition & 27 deletions docs/guide.rst
Expand Up @@ -299,33 +299,7 @@ over which you're iterating.
Here's a simple sketch that traverses all circuits printing their
router IDs, and closing each stream and circuit afterwards:

.. code-block:: python
from twisted.internet.task import react
from twisted.internet.defer import inlineCallbacks
from twisted.internet.endpoints import UNIXClientEndpoint
import txtorcon
@react
@inlineCallbacks
def main(reactor):
"""
Close all open streams and circuits in the Tor we connect to
"""
control_ep = UNIXClientEndpoint(reactor, '/var/run/tor/control')
tor = yield txtorcon.connect(reactor, control_ep)
state = yield tor.create_state()
print("Closing all circuits:")
for circuit in list(state.circuits.values()):
path = '->'.join(map(lambda r: r.id_hex, circuit.path))
print("Circuit {} through {}".format(circuit.id, path))
for stream in circuit.streams:
print(" Stream {} to {}".format(stream.id, stream.target_host))
yield stream.close()
yield circuit.close()
yield tor.quit()
.. literalinclude:: ../examples/close_all_circuits.py


.. _guide_client_use:
Expand Down
27 changes: 27 additions & 0 deletions examples/close_all_circuits.py
@@ -0,0 +1,27 @@
from twisted.internet.task import react
from twisted.internet.defer import inlineCallbacks
from twisted.internet.endpoints import UNIXClientEndpoint

import txtorcon


@react
@inlineCallbacks
def main(reactor):
"""
Close all open streams and circuits in the Tor we connect to
"""
control_ep = UNIXClientEndpoint(reactor, '/var/run/tor/control')
tor = yield txtorcon.connect(reactor, control_ep)
state = yield tor.create_state()
print("Closing all circuits:")
for circuit in list(state.circuits.values()):
path = '->'.join(map(lambda r: r.id_hex, circuit.path))
print("Circuit {} through {}".format(circuit.id, path))
for stream in circuit.streams:
print(" Stream {} to {}".format(stream.id, stream.target_host))
yield stream.close()
print(" closed")
yield circuit.close()
print("closed")
yield tor.quit()

0 comments on commit 3a8a4ae

Please sign in to comment.