[tor-commits] [stem/master] Add a Controller.get_circuits method

atagar at torproject.org atagar at torproject.org
Mon Dec 31 03:13:07 UTC 2012


commit 1b9ffa597408e9b7f0f8863e8c7a2f66d9922e1c
Author: Ravi Chandra Padmala <neenaoffline at gmail.com>
Date:   Wed Dec 5 08:08:18 2012 +0530

    Add a Controller.get_circuits method
---
 stem/control.py                  |   20 ++++++++++++++++++++
 test/integ/control/controller.py |   15 +++++++++++++++
 2 files changed, 35 insertions(+), 0 deletions(-)

diff --git a/stem/control.py b/stem/control.py
index 704bb5e..fe45010 100644
--- a/stem/control.py
+++ b/stem/control.py
@@ -36,6 +36,7 @@ providing its own for interacting at a higher level.
     |- close_stream - close a stream
     |- repurpose_circuit - change a circuit's purpose
     |- map_address - maps one address to another such that connections to the original are replaced with the other
+    |- get_circuits - provides a list of active circuits
     |- get_version - convenience method to get tor version
     |- get_server_descriptor - querying the server descriptor for a relay
     |- get_server_descriptors - provides all presently available server descriptors
@@ -1537,6 +1538,25 @@ class Controller(BaseController):
       raise stem.ProtocolError("EXTENDCIRCUIT returned unexpected response code: %s" % response.code)
     
     return new_circuit
+
+  def get_circuits(self):
+    """
+    Provides the list of circuits Tor is currently handling.
+
+    :returns: **list** of :class:`stem.events.CircuitEvent` objects
+    
+    :raises: :class:`stem.ControllerError` if the call fails
+    """
+     
+    circuits = []
+    response = self.get_info("circuit-status")
+    
+    for circ in response.splitlines():
+      circ_message = stem.socket.recv_message(StringIO.StringIO("650 CIRC " + circ + "\r\n"))
+      stem.response.convert("EVENT", circ_message, arrived_at = 0)
+      circuits.append(circ_message)
+
+    return circuits
   
   def close_circuit(self, circuit_id, flag = ''):
     """
diff --git a/test/integ/control/controller.py b/test/integ/control/controller.py
index 86056aa..9b1f1c6 100644
--- a/test/integ/control/controller.py
+++ b/test/integ/control/controller.py
@@ -722,3 +722,18 @@ class TestController(unittest.TestCase):
         count += 1
         if count > 10: break
 
+  def test_get_circuits(self):
+    """
+    Fetches circuits via the get_circuits() method.
+    """
+    
+    if test.runner.require_control(self): return
+    if test.runner.require_online(self): return
+    
+    runner = test.runner.get_runner()
+    with runner.get_tor_controller() as controller:
+      new_circ = controller.new_circuit()
+      circuits = controller.get_circuits()
+      self.assertTrue(new_circ in [int(circ.id) for circ in circuits])
+  
+





More information about the tor-commits mailing list