File: example.py

package info (click to toggle)
python-torctl 20130920git-2
  • links: PTS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd, stretch
  • size: 396 kB
  • ctags: 814
  • sloc: python: 5,026; makefile: 36
file content (34 lines) | stat: -rw-r--r-- 867 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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