【问题标题】:Selenium WebDriver + Tor as proxy with Stem?Selenium WebDriver + Tor 作为 Stem 的代理?
【发布时间】:2015-04-23 11:37:22
【问题描述】:

我需要确认是否可以使用 Stem 启动暴露 127.0.0.1:port 的 Tor 进程,然后在 selenium 脚本上使用它作为代理 (SOCKS)。

我正在使用 Python 3.4.2、Stem 1.3.0 和 Tor(tor-win32-tor-0.2.5.10 专家 捆绑)在 Windows 上。

这段代码适用于标准 SOCKS 代理。

from selenium import webdriver
from selenium.webdriver.firefox.firefox_profile import FirefoxProfile

profile = FirefoxProfile()
profile.set_preference('network.proxy.type', 1)
profile.set_preference('network.proxy.socks', '127.0.0.1')
profile.set_preference('network.proxy.socks_port', 9000)

driver = webdriver.Firefox(profile)
driver.implicitly_wait(30)
driver.get('http://www.reddit.com')

但我无法让它使用 Tor 作为我的代理。我试图创建一个 Tor 进程,并创建了它。但我真的不知道它是否正常工作。我的tor_error_log.txt 没有错误

# File: stem_process.py
import stem.process
import stem

stem.process.launch_tor_with_config(
  config = {
    'SocksPort': '9000',
    'ControlPort': '9051',
    'ExitNodes': '{us}',
    'Log': [
      'NOTICE stdout',
      'ERR file c:\\tor-win32-tor-0.2.5.10\\Tor\\tor_error_log.txt',
    ],
  },
  tor_cmd = 'C:\\tor-win32-tor-0.2.5.10\\Tor\\tor.exe',
)

然后我尝试了两种方法来创建连接或进行身份验证。第一个是使用withstem.control.controller。第二个在较低级别,stem.socketstem.connection

第一个:

# File: stem_test1.py
from stem.control import Controller

with Controller.from_port(address='127.0.0.1', port=9051) as controller: #port = 9051
  controller.authenticate()

  print("Tor is running version %s" % controller.get_version())

'''
# Output:
Tor is running version 0.2.5.10 (git-13318a95ddfbbf8d)
'''

第二个:

# File: stem_test2.py
import sys
import stem
import stem.connection
import stem.socket

if __name__ == '__main__':
  try:
    control_socket = stem.socket.ControlPort(port = 9051)
    stem.connection.authenticate(control_socket)
  except stem.SocketError as exc:
    print('Unable to connect to tor on port 9051: %s' % exc)
    sys.exit(1)
  except stem.connection.AuthenticationFailure as exc:
    print('Unable to authenticate: %s' % exc)
    sys.exit(1)

  print("Issuing 'GETINFO version' query...\n")
  control_socket.send('GETINFO version')
  print(control_socket.recv())

'''
# Output:
Issuing 'GETINFO version' query...

version=0.2.5.10 (git-13318a95ddfbbf8d)
OK
'''

并且两者都运行没有错误...但是当我使用代码以127.0.0.1:9000作为代理调用Firefox WebDriver实例时(也尝试使用127.0.0.1:9051,因为我真的不知道socksPort之间的区别和controlPort) 它不起作用。

【问题讨论】:

    标签: python selenium proxy tor


    【解决方案1】:

    Stem 无法创建 Tor 进程,它只是一个库,用于通过控制端口连接到现有的 Tor 服务器以进行检查/控制。

    要创建 tor 进程本身,您需要让系统使用 upstart/launchctl/etc 启动它。或者,您可以在命令行中输入tor(如果已安装),它将在前台运行。

    这样,要使用stem,您需要将torrc 编辑为a。启用 ControlPort,然后 b.设置身份验证方法(cookieauth 或存储在您的 torrc 中的哈希密码)。默认的 tor SocksPort 是 9050,ControlPort 是 9051。

    SocksPort 是您路由流量(即 firefox)通过的端口,而 ControlPort 是您连接主干的端口。 请注意,仅当您甚至需要stem时,因为看起来您正试图用它启动一个tor实例(那是不可行的),如果您让它在您的系统香草上运行,它将与selenium/firefox,因为你已经配置好了(嗯,默认端口是 9050 而不是 9000)

    【讨论】:

    • 感谢您的努力,但事实并非如此。 Stem CAN 启动进程,并创建一个正在运行的实例 TOR。您可以使用stem.process.launch_tor_with_config() 创建它,这样您甚至不需要配置文件,只需将配置传递给函数。我是在 Linux 中完成的,但我的问题是针对 Windows 的。你说的端口是正确的。更多:stem.torproject.org/api/process.html
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-08
    • 1970-01-01
    • 2010-12-30
    • 2015-04-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多