当我 运行 同时执行两个脚本时 Tor 失败

Tor Fails when I Run Two Scripts Simultaneously

当我尝试同时 运行 两个相同的脚本时收到以下错误消息:

$ python test_1.py
Traceback (most recent call last):
  File "test_1.py", line 23, in <module>
    'ERR file /tmp/tor_error_log',
  File "/usr/local/lib/python2.7/site-packages/stem/process.py", line 255, in launch_tor_with_config
    return launch_tor(tor_cmd, ['-f', '-'], None, completion_percent, init_msg_handler, timeout, take_ownership, stdin = config_str)
  File "/usr/local/lib/python2.7/site-packages/stem/process.py", line 143, in launch_tor
    raise OSError('Process terminated: %s' % last_problem)
OSError: Process terminated: No, it's still there.  Exiting.

请在下面找到这两个脚本的代码。有人知道如何设置 Tor 以便同时 运行 这两个相同的脚本吗?提前致谢!

from selenium import webdriver
import stem.process
from stem import Signal
from stem.control import Controller
import os

tor_process = stem.process.launch_tor_with_config(
    config = {
        'SocksPort' : '9150',
        'ControlPort' : '9051',
        'Log' : [
            'NOTICE stdout',
            'ERR file /tmp/tor_error_log',
            ],
    },)

service_args = [
    '--proxy=localhost:9150',
    '--proxy-type=socks5',
    ]

driver = webdriver.PhantomJS(service_log_path=os.path.devnull, service_args=service_args)
driver.get('https://check.torproject.org')
print(driver.find_element_by_tag_name('h1').text)
driver.quit()
tor_process.kill()

您需要更改 DataDirectory 设置,this answer 中有示例。

tor_process = stem.process.launch_tor_with_config(
    config = {
        'SocksPort': '7001',
        'ExitNodes': '{ru}',
        'StrictNodes': '1',
        'DataDirectory': r'C:\Tor\Data2',
    },
)

我使用相同的 SocksPort 但不同的 DataDirectory 进行了测试,并且我能够获得不同的退出节点。

请注意,您的错误主要与数据文件夹中存在 lock 文件有关。不要复制那个文件。