Skip to main content
 首页 » 编程设计

python之尝试设置和使用 torctl 但遇到问题

2025年05月04日17haluo1

正在关注 this演练我做了以下事情:

  1. 安装 Tor(手动安装,而不是通过 win-get)
  2. 已安装 privoxy

尝试运行:

tor --hash-password mypassword 

从命令行我得到

tor is not a recognized command... 

我确实安装了tor。

我的目标是在 Python 中通过 torctl 运行 tor - 并且能够更改我的 IP 地址。

运行示例脚本:

from TorCtl import TorCtl 
import urllib2 
 
user_agent = 'Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.7) Gecko/2009021910 Firefox/3.0.7' 
headers={'User-Agent':user_agent} 
 
def request(url): 
    def _set_urlproxy(): 
        proxy_support = urllib2.ProxyHandler({"http" : "127.0.0.1:8118"}) 
        opener = urllib2.build_opener(proxy_support) 
        urllib2.install_opener(opener) 
    _set_urlproxy() 
    request=urllib2.Request(url, None, headers) 
    return urllib2.urlopen(request).read() 
 
def renew_connection(): 
    conn = TorCtl.connect(controlAddr="127.0.0.1", controlPort=9051, passphrase="your_password") 
    conn.send_signal("NEWNYM") 
    conn.close() 
 
for i in range(0, 10): 
    renew_connection() 
    print request("http://icanhazip.com/") 

我得到以下回复:

C:\Users\USER>python C:\temp\py_tor.py 
Failed to establish socket: [Errno 10061] No connection could be made because the target machine actively refused it 
Traceback (most recent call last): 
  File "C:\temp\py_tor.py", line 22, in <module> 
    renew_connection() 
  File "C:\temp\py_tor.py", line 18, in renew_connection 
    conn.send_signal("NEWNYM") 
AttributeError: 'NoneType' object has no attribute 'send_signal' 

请您参考如下方法:

tor --hash-password 表示它不是可识别的命令,因为它不在 Windows 上的 PATH 环境变量中。

只需运行 C:\tor\tor.exe 将 C:\tor 替换为您安装它的位置。

此外,由于您在 Windows 上,运行 tor --hash-password password 不会显示任何输出,除非您将其通过管道传输到 more,因此您应该使用:

C:\tor\tor.exe --hash-password PASSWORD | more 

错误10061是因为默认情况下Tor不监听控制端口9051所以你需要编辑torrc配置文件和添加 ControlPort 9051,这样它将监听您将用来发出 NEWNYM 信号以切换电路(更改 IP)的控制连接。