0

I started with TOR's STEM library and I am trying to make the following code work but can't figure out the reason for the error.

Here's my code:


import stem
from stem.process import launch_tor
from stem.control import Controller




tor_path = ".\\Tor Browser\\Browser\\TorBrowser\\Tor\\tor.exe"

def print_lines(line):
    if('Bootstrapped' in line):
        print(line)

tor = launch_tor(tor_cmd = tor_path, init_msg_handler = print_lines)

try:
    controller = Controller.from_port()
except stem.SocketError as exc:
    print("Unable to connect to tor on port 9051: %s" % exc)




tor.kill()

And I am getting the following error:


Unable to connect to tor on port 9051: [WinError 10061] No connection could be made because the target machine actively refused it

1 Answer 1

1

No ControlPort is specified in the torrc that your copy of Tor is trying to read.

ControlPort PORT|unix:path|auto [flags]

...

(Default: 0)

With the default of 0 meaning not to create one.

Instead either specify one in the place that tor.exe looks for it's torrc by default or use launch_tor_with_config instead.

For example:

from stem.process import launch_tor_with_config
from stem.control import Controller

tor_path = ".\\Tor Browser\\Browser\\TorBrowser\\Tor\\tor.exe"
def print_lines(line):
    if('Bootstrapped' in line):
        print(line)
tor = launch_tor_with_config(tor_cmd = tor_path, init_msg_handler = print_lines, config = {'ControlPort': '9051'})
try:
    controller = Controller.from_port()
except stem.SocketError as exc:
    print("Unable to connect to tor on port 9051: %s" % exc)
tor.terminate() # It is better to send a SIGTERM, since Tor will handle this and ensure it exits cleanly.
1
  • thanks! it worked... apart from the stem documentation, is there any web resource that I can use?
    – sigmapie8
    Jun 12, 2017 at 11:21

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .