I am attempting to start a Tor connection using Stem
and then list the nodes that are selected.
Using other questions and the FAQ on their website I can do one or other, but not both.
For example, I can start a Tor circuit with
tor_process = stem.process.launch_tor_with_config(
config = {
'ControlPort': '2778',
'Log': [
'NOTICE stdout',
'ERR file /tmp/tor_error_log',
],
"MaxCircuitDirtiness" : str(60*60*24*30),
"NewCircuitPeriod" : str(60*60*24*30),
},
)
And separately I can look at the circuit nodes if I start Tor separately using code from here.
But if I try to do both, such as:
from stem import CircStatus
from stem.control import Controller
import stem
import stem.connection
import stem.process
from stem.control import Controller
if __name__ == '__main__':
tor_process = stem.process.launch_tor_with_config(
config = {
'ControlPort': '2778',
'Log': [
'NOTICE stdout',
'ERR file /tmp/tor_error_log',
],
"MaxCircuitDirtiness" : str(60*60*24*30),
"NewCircuitPeriod" : str(60*60*24*30),
},
)
with Controller.from_port() as controller:
controller.authenticate()
for circ in controller.get_circuits():
if circ.status != CircStatus.BUILT:
continue # skip circuits that aren't yet usable
entry_fingerprint = circ.path[0][0]
entry_descriptor = controller.get_network_status(entry_fingerprint, None)
if entry_descriptor:
print "Circuit %s starts with %s" % (circ.id, entry_descriptor.address)
else:
print "Unable to determine the address belonging to circuit %s" % circ.id
tor_process.kill()
I get an error:
Traceback (most recent call last):
File "circuit.py", line 22, in <module>
with Controller.from_port() as controller:
File "/usr/local/lib/python2.7/dist-packages/stem/control.py", line 1024, in from_port
control_port = stem.connection._connection_for_default_port(address)
File "/usr/local/lib/python2.7/dist-packages/stem/connection.py", line 1058, in _connection_for_default_port
raise exc
stem.SocketError: [Errno 111] Connection refused
Which is normally the error I get when I try to do things without having separately started tor
.
Is there a way to combine these two operations?
EDIT: There is, I forgot I set the ControlPort
answered my question below.
The answer was simple, I forgot I set the port in config
, I set ControlPort : 2778
, but opened Contoller.from_port()
which uses the default port of 9051.
Using Controller.from_port(port = 2778)
fixed the problem.