[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[tor-commits] [pytorctl/master] Replacing old TorCtl example with a BW listener



commit 257fedbbe7b4e8f066ba570773bb2f2afbca6dc4
Author: Damian Johnson <atagar@xxxxxxxxxxxxxx>
Date:   Sun Jun 12 22:02:14 2011 -0700

    Replacing old TorCtl example with a BW listener
    
    Event listening, particularly BW events, is a common request on irc so using it
    as the TorCtl example. For more information see bug 2065.
---
 TorCtl.py  |   56 ++------------------------------------------------------
 example.py |   34 ++++++++++++++++++++++++++++++++++
 2 files changed, 36 insertions(+), 54 deletions(-)

diff --git a/TorCtl.py b/TorCtl.py
index 8fac3df..2758033 100755
--- a/TorCtl.py
+++ b/TorCtl.py
@@ -9,8 +9,8 @@ Library to control Tor processes.
 This library handles sending commands, parsing responses, and delivering
 events to and from the control port. The basic usage is to create a
 socket, wrap that in a TorCtl.Connection, and then add an EventHandler
-to that connection. A simple example with a DebugEventHandler (that just
-echoes the events back to stdout) is present in run_example().
+to that connection. For a simple example that prints our BW events (events
+that provide the amount of traffic going over tor) see 'example.py'.
 
 Note that the TorCtl.Connection is fully compatible with the more
 advanced EventHandlers in TorCtl.PathSupport (and of course any other
@@ -1837,55 +1837,3 @@ def parseHostAndPort(h):
 
   return host, port
 
-def run_example(host,port):
-  """ Example of basic TorCtl usage. See PathSupport for more advanced
-      usage.
-  """
-  print "host is %s:%d"%(host,port)
-  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
-  s.connect((host,port))
-  c = Connection(s)
-  c.set_event_handler(DebugEventHandler())
-  th = c.launch_thread()
-  c.authenticate()
-  print "nick",`c.get_option("nickname")`
-  print `c.get_info("version")`
-  #print `c.get_info("desc/name/moria1")`
-  print `c.get_info("network-status")`
-  print `c.get_info("addr-mappings/all")`
-  print `c.get_info("addr-mappings/config")`
-  print `c.get_info("addr-mappings/cache")`
-  print `c.get_info("addr-mappings/control")`
-
-  print `c.extend_circuit(0,["moria1"])`
-  try:
-    print `c.extend_circuit(0,[""])`
-  except ErrorReply: # wtf?
-    print "got error. good."
-  except:
-    print "Strange error", sys.exc_info()[0]
-   
-  #send_signal(s,1)
-  #save_conf(s)
-
-  #set_option(s,"1")
-  #set_option(s,"bandwidthburstbytes 100000")
-  #set_option(s,"runasdaemon 1")
-  #set_events(s,[EVENT_TYPE.WARN])
-#  c.set_events([EVENT_TYPE.ORCONN], True)
-  c.set_events([EVENT_TYPE.STREAM, EVENT_TYPE.CIRC,
-          EVENT_TYPE.NEWCONSENSUS, EVENT_TYPE.NEWDESC,
-          EVENT_TYPE.ORCONN, EVENT_TYPE.BW], True)
-
-  th.join()
-  return
-
-if __name__ == '__main__':
-  if len(sys.argv) > 2:
-    print "Syntax: TorControl.py torhost:torport"
-    sys.exit(0)
-  else:
-    sys.argv.append("localhost:9051")
-  sh,sp = parseHostAndPort(sys.argv[1])
-  run_example(sh,sp)
-
diff --git a/example.py b/example.py
new file mode 100644
index 0000000..fdb1a99
--- /dev/null
+++ b/example.py
@@ -0,0 +1,34 @@
+"""
+The following is a simple example of TorCtl usage. This attaches a listener
+that prints the amount of traffic going over tor each second.
+"""
+
+import time
+import TorCtl
+
+class BandwidthListener(TorCtl.PostEventListener):
+  def __init__(self):
+    TorCtl.PostEventListener.__init__(self)
+
+  def bandwidth_event(self, event):
+    print "tor read %i bytes and wrote %i bytes" % (event.read, event.written)
+
+# constructs a listener that prints BW events
+myListener = BandwidthListener()
+
+# initiates a TorCtl connection, returning None if it was unsuccessful
+conn = TorCtl.connect()
+
+if conn:
+  # tells tor to send us BW events
+  conn.set_events(["BW"])
+
+  # attaches the listener so it'll receive BW events
+  conn.add_event_listener(myListener)
+
+  # run until we get a keyboard interrupt
+  try:
+    while True:
+      time.sleep(10)
+  except KeyboardInterrupt: pass
+

_______________________________________________
tor-commits mailing list
tor-commits@xxxxxxxxxxxxxxxxxxxx
https://lists.torproject.org/cgi-bin/mailman/listinfo/tor-commits