“stem.SocketError: [WinError 10061] 无法建立连接,因为目标机器主动拒绝它”通过 Selenium、Python、Firefox 用于 Tor

问题描述

我正在尝试通过 Python 使用 Tor。我可以打开一个窗口,但不能创建一个新的身份。到目前为止,这是我的代码

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile
from selenium.webdriver import FirefoxOptions
from selenium.webdriver.support.ui import webdriverwait
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time,random,os
from seleniumrequests import Firefox
from tbselenium.tbdriver import TorbrowserDriver
from stem import Signal
from stem.control import Controller

def tor_browser():
        torexe = os.popen(r'C:/Users/USER/Desktop/Torbrowser/browser/Torbrowser/Tor/tor.exe')
        global firefox_profile
        profile = FirefoxProfile(r'C:\USER\Desktop\Torbrowser\browser\Torbrowser\Data\browser\profile.default')
        profile.set_preference('network.proxy.type',1)
        profile.set_preference('network.proxy.socks','127.0.0.1')
        profile.set_preference('network.proxy.socks_port',9050)
        profile.set_preference("network.proxy.socks_remote_dns",False)
        profile.update_preferences()
        driver = webdriver.Firefox(firefox_profile=profile)
        driver.get("http://check.torproject.org")
        time.sleep(5)
        with Controller.from_port(port = 9051) as controller:
            controller.authenticate()
            controller.signal(Signal.NEWNYM)
        driver.get("http://check.torproject.org")
        time.sleep(5)
        driver.quit()

这是我的回溯:

Traceback (most recent call last):
  File "C:\Users\USER\AppData\Local\Programs\Python\python38-32\lib\site-packages\stem\socket.py",line 535,in _make_socket
    control_socket.connect((self.address,self.port))
ConnectionRefusedError: [WinError 10061] No connection Could be made because the target machine actively refused it

During handling of the above exception,another exception occurred:

Traceback (most recent call last):
  File "c:\Users\USER\Documents\Python Files\New Selenium\seleniumVariants.py",line 153,in <module>
    tor_browser()
  File "c:\Users\USER\Documents\Python Files\New Selenium\seleniumVariants.py",line 56,in tor_browser
    with Controller.from_port(port = 9051) as controller:
  File "C:\Users\USER\AppData\Local\Programs\Python\python38-32\lib\site-packages\stem\control.py",line 1033,in from_port
    control_port = stem.socket.ControlPort(address,port)
  File "C:\Users\USER\AppData\Local\Programs\Python\python38-32\lib\site-packages\stem\socket.py",line 503,in __init__
    self.connect()
  File "C:\Users\USER\AppData\Local\Programs\Python\python38-32\lib\site-packages\stem\socket.py",line 172,in connect
    self._socket = self._make_socket()
  File "C:\Users\USER\AppData\Local\Programs\Python\python38-32\lib\site-packages\stem\socket.py",line 538,in _make_socket
    raise stem.socketError(exc)
stem.socketError: [WinError 10061] No connection Could be made because the target machine actively refused it

我在 C:\Users\USER\Desktop\Tor browser\browser\Torbrowser\Data\Tor 处检查了我的 torrc 文件,但它完全是空白的,没有一个字符,所以我在其中添加了以下几行:

  • ControlPort 9051
  • SocksListenAddress 0.0.0.0:9050
  • CookieAuthentication 1

torrc-defaults 的结尾一样,但只有几行。我还在 Windows 防火墙中添加一个新规则以允许通过端口 9051 的入站和出站连接,但这仍然不起作用。我已经搜索了所有地方,包括这里,但没有找到任何东西。

提前致谢!

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...