Tom Tom - 1 year ago 190
Python Question

Choose specific exit node in TOR (with Python code)

I want to write a simple script in Python that will connect to the TOR network and choose an exit node of some country I specify. For instance I want all my outgoing network traffic to be routed to say - spain (through the TOR network). Is there some way to achieve this?


Tom Tom
Answer Source

Ok i think i have an answer, it is not exactly what i intended but it works just as well.

I can have a more then one TOR relays on my machine and configure each one with the exit node i want in the torrc file, like Robert Davey mentioned. In my python script i can create a few processes and connect each one with a different relay - thus giving me a different exit node. An example can be found here:

If i want to change an exit node in an already running relay i can edit the torrc file of the relevant TOR relay and update it with the SIGHUP signal using stem python library:

from stem import Signal
from stem.control import Controller

with Controller.from_port(port = 9051) as controller: