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

时间:2017-07-05 22:06:59

标签: python tor

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

$ 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()

1 个答案:

答案 0 :(得分:0)

您需要更改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文件的存在有关。不要复制该文件。