Search code examples
pythontor

Tor Fails when I Run Two Scripts Simultaneously


I'm getting the following error message when I try to run two equal scripts at the same time:

$ 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.

Please find the code for both scripts below. Does anybody knows how can I set up Tor in order to run this two equal scripts simultaneously? Thanks in advance!

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

Solution

  • You need to change the DataDirectory setting, there's an example in this answer.

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

    I tested with the same SocksPort but different DataDirectory and I'm able to get different Exit Nodes.

    Note that your error mostly have to do with the presence of the lock file in the data folder. Don't copy that file.