You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Windows11下用Stem库更新Tor IP时遇[WinError10061]连接拒绝问题

问题描述

在Windows 11系统中使用Python的Stem库自动更新Tor IP地址时,持续遇到以下错误:

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

用户代码

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 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
问题分析

从控制台输出的Configuration file "C:\Users\anjoyer\AppData\Roaming\tor\torrc" not present可以看出,Tor并未加载到你配置的torrc文件,导致ControlPort 9051的设置未生效,因此Stem无法连接到控制端口,出现连接被拒绝的错误。另外,你的torrc文件中HashedControlPassword的配置也是错误的,需要生成实际的哈希密码。

解决方案

1. 确保Tor加载正确的torrc文件

Tor默认会读取C:\Users\anjoyer\AppData\Roaming\tor\torrc路径下的配置文件:

  • 检查该路径下是否存在torrc文件,如果没有,将你配置的torrc文件复制到这个路径;
  • 或者在启动Tor时,通过--config-file参数指定你的torrc文件位置。

2. 生成正确的哈希密码

打开命令提示符,执行以下命令生成密码哈希(将your_password替换为你要设置的密码):

"C:\Users\anjoyer\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe" --hash-password your_password

将输出的哈希字符串替换到torrc文件中,例如:

ControlPort 9051
HashedControlPassword 16:ABC123...

3. 修改启动Tor的代码

修改start_tor函数,添加配置文件参数,并延长启动等待时间:

def start_tor():
    try:
        tor_path = r"C:\Users\anjoyer\Desktop\Tor Browser\Browser\TorBrowser\Tor\tor.exe"
        # 替换为你的torrc实际路径
        torrc_path = r"C:\Users\anjoyer\AppData\Roaming\tor\torrc"
        subprocess.Popen([tor_path, '--config-file', torrc_path])
        print("Tor started successfully.")
        time.sleep(10)  # 延长等待时间,确保Tor完全加载控制端口
    except Exception as e:
        print(f"Error starting Tor: {e}")

4. 修正认证密码

代码中controller.authenticate需要使用你设置的明文密码(Tor会自动验证哈希),修改renew_tor_ip函数:

def renew_tor_ip():
    try:
        with Controller.from_port(port=9051) as controller:
            controller.authenticate(password='your_password')  # 替换为你设置的明文密码
            controller.signal(Signal.NEWNYM)
            print("Tor IP renewed successfully.")
    except Exception as e:
        print(f"Error renewing Tor IP: {e}")

5. 验证控制端口是否开启

Tor启动后,打开命令提示符执行netstat -ano | findstr :9051,如果能看到监听记录,说明控制端口已正常开启。

内容的提问来源于stack exchange,提问作者anjoyer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.21 22:05:02