OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Trouble renewing Tor IP address on Windows 11 using stem library

  • Thread starter Thread starter Castle Thief thief cake
  • Start date Start date
C

Castle Thief thief cake

Guest
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:

Code:
Error renewing Tor IP: [WinError 10061] No connection could be made because the target computer actively refused it

My Code:

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:

Code:
ControlPort 9051
HashedControlPassword HashedControlPassword

Tor starts successfully when I execute my script, and the console outputs the following information:

Code:
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.
<p>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:</p>
<pre><code>Error renewing Tor IP: [WinError 10061] No connection could be made because the target computer actively refused it
</code></pre>
<p>My Code:</p>
<pre><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()

</code></pre>
<p>torrc:</p>
<pre><code>ControlPort 9051
HashedControlPassword HashedControlPassword
</code></pre>
<p>Tor starts successfully when I execute my script, and the console outputs the following information:</p>
<pre><code>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
</code></pre>
<p>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.</p>
 

Latest posts

Top