Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/296.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/selenium/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/xcode/7.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python Selenium WebDriver+;Tor作为Stem的代理?_Python_Selenium_Proxy_Tor - Fatal编程技术网

Python Selenium WebDriver+;Tor作为Stem的代理?

Python Selenium WebDriver+;Tor作为Stem的代理?,python,selenium,proxy,tor,Python,Selenium,Proxy,Tor,我需要确认Stem是否可以用于启动公开127.0.0.1:port的Tor进程,然后将其作为代理(SOCKS)在selenium脚本上使用 我正在使用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 Firef

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

我正在使用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\u error\u 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',
)
然后我尝试了两种方法来创建连接或进行身份验证。第一种是将
stem.control.controller一起使用。第二个在较低级别,带有
阀杆套筒
阀杆连接

第一个:

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

两个都运行正常。。。但是当我使用代码调用Firefox WebDriver实例时,使用
127.0.0.1:9000
作为代理(也尝试使用
127.0.0.1:9051
,因为我不知道
socksPort
controlPort
之间的区别),它不起作用。

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

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

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

SocksPort是您将流量(即firefox)路由到的端口,ControlPort是您连接到的端口。
请注意,只有当您甚至需要stem时,因为您似乎正试图用它启动tor实例(这是不可能的),如果您在系统上运行它,它将在您配置的selenium/firefox上工作(默认端口是9050而不是9000)

谢谢您的努力,但这不是真的。Stem可以启动流程,并创建一个正在运行的实例。您可以使用
stem.process.launch\u tor\u with_config()
创建它。这样,您甚至不需要配置文件,而是将配置传递给函数。我是在Linux上做的,但我的问题是针对Windows的。你说的关于端口的话是正确的。更多: