如何解决“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 举报,一经查实,本站将立刻删除。