[tor-commits] [stem/master] Adjust example of writing to disk

atagar at torproject.org atagar at torproject.org
Sat Aug 17 20:44:27 UTC 2019


commit 1aa6117bcbda629727e7351b2abb88b7d5f1a697
Author: Damian Johnson <atagar at torproject.org>
Date:   Fri Jul 5 12:37:37 2019 -0700

    Adjust example of writing to disk
    
    On reflection I won't add special functions for downloading descriptors to
    disk. That's uncommon, and takes a couple lines for users to do themselves.
    Adjusting our example to show how.
---
 stem/descriptor/collector.py | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/stem/descriptor/collector.py b/stem/descriptor/collector.py
index 0bbf4251..746e4b1d 100644
--- a/stem/descriptor/collector.py
+++ b/stem/descriptor/collector.py
@@ -29,17 +29,20 @@ With this you can either download and read directly from CollecTor...
 ::
 
   import datetime
+  import os
   import stem.descriptor
   import stem.descriptor.collector
 
   yesterday = datetime.date.today() - datetime.timedelta(1)
+  path = os.path.expanduser('~/descriptor_cache/server_desc_today')
 
-  stem.descriptor.collector.download_server_descriptors(
-    destination = '~/descriptor_cache',
-    start = yesterday,
-  ).join()
+  with open(path, 'wb') as cache_file:
+    for desc in stem.descriptor.collector.get_server_descriptors(start = yesterday):
+      cache_file.write(desc.get_bytes())
 
-  for desc in stem.descriptor.parse_file('~/descriptor_cache', descriptor_type = 'server-descriptor 1.0'):
+  # then later...
+
+  for desc in stem.descriptor.parse_file(path, descriptor_type = 'server-descriptor 1.0'):
     if desc.exit_policy.is_exiting_allowed():
       print('  %s (%s)' % (desc.nickname, desc.fingerprint))
 





More information about the tor-commits mailing list