简体   繁体   English

Selenium WebDriver + Tor作为Stem的代理?

[英]Selenium WebDriver + Tor as proxy with Stem?

I need to confirm If Stem can be used to launch a Tor process that exposes 127.0.0.1:port, then use it on selenium scripts as proxy (SOCKS). 我需要确认If Stem是否可用于启动暴露127.0.0.1:port的Tor进程,然后在selenium脚本上使用它作为代理(SOCKS)。

I'm using Python 3.4.2 , Stem 1.3.0, and Tor (tor-win32-tor-0.2.5.10 expert bundle) on Windows. 我在Windows上使用Python 3.4.2,Stem 1.3.0和Tor(tor-win32-tor-0.2.5.10专家包)。

This piece of code works with a standard SOCKS proxy. 这段代码适用于标准的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')

But I can't manage to get it working with Tor as my proxy. 但我无法让它与Tor合作作为我的代理。 I tried to create a Tor process, and its created. 我试图创建一个Tor进程,并创建它。 But I don't really know If it's working properly. 但我真的不知道它是否正常工作。 I don't get errors in my tor_error_log.txt 我的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',
)

Then I tried two ways to create the connection or authenticate. 然后我尝试了两种方法来创建连接或进行身份验证。 The first one is using with and stem.control.controller . 第一个是使用withstem.control.controller And the second at lower level with stem.socket and stem.connection 第二个是较低级别的stem.socketstem.connection

The first one: 第一个:

# 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)
'''

The second one: 第二个:

# 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
'''

And both run without errors... But when I use the code to call the Firefox WebDriver instance with 127.0.0.1:9000 as proxy (also tried with 127.0.0.1:9051 , because I don't really know the difference between socksPort and controlPort ) It doesn't work. 并且两者都运行没有错误......但是当我使用代码以127.0.0.1:9000作为代理调用Firefox WebDriver实例时(也尝试使用127.0.0.1:9051 ,因为我真的不知道socksPortcontrolPort )它不起作用。

Stem can't create a tor process, its only a library for connecting to an existing tor server for inspection/control via the control port. Stem不能创建一个tor进程,它只是一个库,用于通过控制端口连接到现有的tor服务器进行检查/控制。

To create the tor process itself, you need to have your system start it up with upstart/launchctl/etc. 要创建tor进程本身,您需要让系统使用upstart / launchctl / etc启动它。 Alternatively, you can just type tor from the commandline if its installed and it'll run in the foreground. 或者,您可以从命令行键入tor ,如果已安装它,它将在前台运行。

With that, to use stem you'll need to edit your torrc to a. 有了它,要使用干,你需要编辑你的torrc到。 enable the ControlPort, and b. 启用ControlPort,和b。 set an authentication method (cookieauth or hashed password stored in your torrc). 设置一个身份验证方法(存储在你的torrc中的cookieauth或哈希密码)。 The default tor SocksPort is 9050 and ControlPort is 9051. 默认的tor SocksPort是9050,ControlPort是9051。

The SocksPort is the one you route your traffic (ie firefox) through, the ControlPort is what you connect stem to. SocksPort是您路由流量的那个(即firefox),ControlPort是您连接到的端口。 Mind you, only if you even need stem, since it just seems like you're trying to start a tor instance with it (and thats not do-able), if you get it running on your system vanilla, it'll work with selenium/firefox as you've got it configured (well, default port is 9050 not 9000) 请注意,只有你甚至需要干,因为它似乎你正试图用它启动一个tor实例(而且这是不可行的),如果你让它在你的系统香草上运行,它将适用于你配置了selenium / firefox(好吧,默认端口是9050而不是9000)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM