[tor-commits] [stem/master] Using get_server_descriptors() in the tutorial

atagar at torproject.org atagar at torproject.org
Tue Oct 16 16:06:33 UTC 2012


commit a0470a6285c2462535f53536e7ed969725356049
Author: Damian Johnson <atagar at torproject.org>
Date:   Tue Oct 16 09:03:27 2012 -0700

    Using get_server_descriptors() in the tutorial
    
    Using the controller's new get_server_descriptors() method to exemplify an
    alternate way of doing the 'Mirror Mirror on the Wall' tutorial without reading
    the cached files directly. Both methods have advantages so I definitely want to
    exemplify each.
---
 docs/tutorial.rst     |   29 ++++++++++++++++++++++++++---
 test/unit/tutorial.py |    4 +---
 2 files changed, 27 insertions(+), 6 deletions(-)

diff --git a/docs/tutorial.rst b/docs/tutorial.rst
index 9cb1414..c6f8d18 100644
--- a/docs/tutorial.rst
+++ b/docs/tutorial.rst
@@ -95,12 +95,10 @@ To read this file we'll use the :class:`~stem.descriptor.reader.DescriptorReader
       if desc.exit_policy.is_exiting_allowed():
         bw_to_relay.setdefault(desc.observed_bandwidth, []).append(desc.nickname)
   
-  sorted_bw = sorted(bw_to_relay.keys(), reverse = True)
-  
   # prints the top fifteen relays
   
   count = 1
-  for bw_value in sorted_bw:
+  for bw_value in sorted(bw_to_relay.keys(), reverse = True):
     for nickname in bw_to_relay[bw_value]:
       print "%i. %s (%i bytes/s)" % (count, nickname, bw_value)
       count += 1
@@ -127,3 +125,28 @@ To read this file we'll use the :class:`~stem.descriptor.reader.DescriptorReader
   14. politkovskaja2 (26149682 bytes/s)
   15. wau (25929953 bytes/s)
 
+This can be easily done through the controller too...
+
+::
+
+  import sys 
+  from stem.control import Controller
+  
+  bw_to_relay = {} # mapping of observed bandwidth to the relay nicknames
+  
+  with Controller.from_port(control_port = 9051) as controller:
+    controller.authenticate()
+    
+    for desc in controller.get_server_descriptors():
+      if desc.exit_policy.is_exiting_allowed():
+        bw_to_relay.setdefault(desc.observed_bandwidth, []).append(desc.nickname)
+  
+  count = 1 
+  for bw_value in sorted(bw_to_relay.keys(), reverse = True):
+    for nickname in bw_to_relay[bw_value]:
+      print "%i. %s (%i bytes/s)" % (count, nickname, bw_value)
+      count += 1
+      
+      if count > 15: 
+        sys.exit()
+
diff --git a/test/unit/tutorial.py b/test/unit/tutorial.py
index 13ef645..72ea28c 100644
--- a/test/unit/tutorial.py
+++ b/test/unit/tutorial.py
@@ -60,12 +60,10 @@ class TestTutorial(unittest.TestCase):
         if desc.exit_policy.is_exiting_allowed():
           bw_to_relay.setdefault(desc.observed_bandwidth, []).append(desc.nickname)
     
-    sorted_bw = sorted(bw_to_relay.keys(), reverse = True)
-    
     # prints the top fifteen relays
     
     count = 1
-    for bw_value in sorted_bw:
+    for bw_value in sorted(bw_to_relay.keys(), reverse = True:
       for nickname in bw_to_relay[bw_value]:
         expected_line = "%i. speedyexit (104590 bytes/s)" % count
         printed_line = "%i. %s (%i bytes/s)" % (count, nickname, bw_value)





More information about the tor-commits mailing list