Although the Stem script "exit_used.py" runs for a while, eventually it hits "Exception in thread Event Notifier" and stops. Traceback reports that the failing call is:

  File "/usr/local/lib/python2.7/dist-packages/stem/control.py", line 2196, in get_circuit
    raise exc
ValueError: Tor presently does not have a circuit with the id of 'None'

If I knew Python, I would just trap all "circuit ID = None" errors, and move along. But I don't.

How might I accomplish that?

Background: I'd like to use the Stem script "exit_used.py" to log Tor exits being used while testing load times for various websites (torsocks midori -s https://foo.com). I got the idea from grarpamp's post "Craigslist now giving Tor the slows, lol" in tor-talk.


I've added a timestamp, and reformatted the output for easy spreadsheet entry.


import functools
import time
from stem import StreamStatus
from stem.control import EventType, Controller

def main():
  print "Tracking requests for tor exits. Press 'enter' to end."

  with Controller.from_port() as controller:

    stream_listener = functools.partial(stream_event, controller)
    controller.add_event_listener(stream_listener, EventType.STREAM)

    raw_input()  # wait for user to press enter

def stream_event(controller, event):
  if event.status == StreamStatus.SUCCEEDED and event.circ_id:
    circ = controller.get_circuit(event.circ_id)
    exit_fingerprint = circ.path[-1][0]
    exit_relay = controller.get_network_status(exit_fingerprint)
    t = time.localtime()

    print "datetime|%d-%02d-%02d %02d:%02d:%02d % (t.tm_year, t.tm_mon, t.tm_mday, t.tm_hour, t.tm_min, t.tm_sec)
    print "website|%s" % (event.target)
    print "exitip|%s" % (exit_relay.address)
    print "exitport|%i" % (exit_relay.or_port)
    print "fingerprint|%s" % exit_relay.fingerprint
    print "nickname|%s" % exit_relay.nickname
    print "locale|%s" % controller.get_info("ip-to-country/%s" % exit_relay.address, 'unknown')

if __name__ == '__main__':

Thanks for the catch! Fixed...


  • Thank you! I should have just created a ticket at <trac.torproject.org/projects/tor/wiki/doc/stem/bugs>, right? Also, how easy would it be to print a timestamp? I know that I need to import "datetime.datetime", and then print "datetime.datetime.now()" in format "%Y-%m-%d %H:%M:%S". But just how is a mystery to me.
    – mirimir
    Jun 4 '14 at 20:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.