I'm trying to automate the renewal of Tor IP addresses using Python on Windows 11 with the stem library. However, I keep encountering the following error:
Error renewing Tor IP: [WinError 10061] No connection could be made because the target computer actively refused it
My Code:
import subprocess # Import subprocess module for starting Tor
from stem import Signal
from stem.control import Controller
import requests
import shutil
import os
import time
import sys
def start_tor():
try:
tor_path = r"C:\Users\anjoyer\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe"
subprocess.Popen([tor_path])
print("Tor started successfully.")
time.sleep(5) # Give Tor some time to start up (adjust as needed)
except Exception as e:
print(f"Error starting Tor: {e}")
def renew_tor_ip():
try:
with Controller.from_port(port=9051) as controller:
controller.authenticate(password='HashedControlPassword')
controller.signal(Signal.NEWNYM)
print("Tor IP renewed successfully.")
except Exception as e:
print(f"Error renewing Tor IP: {e}")
proxies = {
'http': 'socks5h://127.0.0.1:9050',
'https': 'socks5h://127.0.0.1:9050'
}
def get_new_ip():
renew_tor_ip()
response = requests.get('http://httpbin.org/ip', proxies=proxies)
return response.json()
if __name__ == "__main__":
start_tor() # Start Tor
renew_tor_ip()
while True:
time.sleep(300)
renew_tor_ip()
torrc:
ControlPort 9051
HashedControlPassword HashedControlPassword
Tor starts successfully when I execute my script, and the console outputs the following information:
Tor started successfully.
Jun 23 19:25:59.793 [notice] Tor 0.4.8.12 (git-2beaa7557c3c93ec) running on Windows 8 [or later] with Libevent 2.1.12-stable, OpenSSL 3.0.14, Zlib 1.3.1, Liblzma N/A, Libzstd N/A and Unknown N/A as libc.
Jun 23 19:25:59.793 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://support.torproject.org/faq/staying-anonymous/
Jun 23 19:25:59.801 [notice] Configuration file "C:\Users\anjoyer\AppData\Roaming\tor\torrc" not present, using reasonable defaults.
Jun 23 19:25:59.801 [warn] Path for GeoIPFile (<default>) is relative and will resolve to C:\Users\anjoyer\desktop\<default>. Is this what you wanted?
Jun 23 19:25:59.801 [warn] Path for GeoIPv6File (<default>) is relative and will resolve to C:\Users\anjoyer\desktop\<default>. Is this what you wanted?
Jun 23 19:25:59.803 [notice] Opening Socks listener on 127.0.0.1:9050
Jun 23 19:25:59.803 [notice] Opened Socks listener connection (ready) on 127.0.0.1:9050
Jun 23 19:25:59.000 [notice] Bootstrapped 0% (starting): Starting
Jun 23 19:26:00.000 [notice] Starting with guard context "default"
Jun 23 19:26:01.000 [notice] Bootstrapped 5% (conn): Connecting to a relay
Jun 23 19:26:01.000 [notice] Bootstrapped 10% (conn_done): Connected to a relay
Jun 23 19:26:01.000 [notice] Bootstrapped 14% (handshake): Handshaking with a relay
Jun 23 19:26:02.000 [notice] Bootstrapped 15% (handshake_done): Handshake with a relay done
Jun 23 19:26:02.000 [notice] Bootstrapped 75% (enough_dirinfo): Loaded enough directory info to build circuits
Jun 23 19:26:02.000 [notice] Bootstrapped 90% (ap_handshake_done): Handshake finished with a relay to build circuits
Jun 23 19:26:02.000 [notice] Bootstrapped 95% (circuit_create): Establishing a Tor circuit
Jun 23 19:26:03.000 [notice] Bootstrapped 100% (done): Done
Error renewing Tor IP: [WinError 10061] No connection could be made because the target computer actively refused it
I've configured the torrc file with ControlPort 9051 and HashedControlPassword. I've started Tor manually and verified it's running. I've checked my firewall settings to ensure Tor has permissions on port 9051 and tor.exe.
try/except
to see FULL error messagesleep()
?controller.authenticate()
in code, and withoutHashedControlPassword
- if it will work (or at least it will show different error) then you have wrong password. As I remember it torrc it has to be hashed but in code in plain text.