torproject / stem

Python controller library for Tor
https://stem.torproject.org/
GNU Lesser General Public License v3.0
257 stars 75 forks source link

Outdated example code #68

Closed woswos closed 3 years ago

woswos commented 4 years ago

Hi again,

There are two issues with this page https://stem.torproject.org/api/descriptor/microdescriptor.html

Firstly, the ticket #7953 seems to be already closed. I think "To do so you need to match the microdescriptor's digest against its corresponding router status entry. For added fun as of this writing the controller doesn't even surface those router status entries (ticket 7953)." part mentioned in the description is already implemented.

Secondly, the first example on that page doesn't work anymore since version 1.8.0 (as @atagar mentioned in the changelog: Replaced our digest attribute with a much more flexible digest() method. Unfortunately I cannot do this in a backward compatible way because of the name conflict.)

Here is the updated version of the example that works with the latest Stem:

import os

from stem.control import Controller
from stem.descriptor import parse_file

with Controller.from_port(port = 9051) as controller:
  controller.authenticate()

  exit_digests = set()
  data_dir = controller.get_conf('DataDirectory')

  for desc in controller.get_microdescriptors():
    if desc.exit_policy.is_exiting_allowed():
      exit_digests.add(desc.digest())

  print 'Exit Relays:'

  for desc in parse_file(os.path.join(data_dir, 'cached-microdesc-consensus')):
    if desc.microdescriptor_digest in exit_digests:
      print '  %s (%s)' % (desc.nickname, desc.fingerprint)

Thank you for creating this library, it is super useful!

atagar commented 3 years ago

Hi Barkin, sorry for the long delay and thanks for pointing this out! Fix pushed...

https://gitweb.torproject.org/stem.git/commit/?id=1731720