pythontorstem

Tor failing to run with Failed to bind one of the listener ports


When I run the [example][1] from the stem documentation i get the error:

OSError: Process terminated: Failed to bind one of the listener ports.

The exact code that I am running is below:

import socks
import socket
import stem.process
import urllib

from stem.util import term

SOCKS_PORT = 7000

# Set socks proxy and wrap the urllib module

socks.setdefaultproxy(socks.PROXY_TYPE_SOCKS5, '127.0.0.1', SOCKS_PORT)
socket.socket = socks.socksocket

# Perform DNS resolution through the socket

def getaddrinfo(*args):
  return [(socket.AF_INET, socket.SOCK_STREAM, 6, '', (args[0], args[1]))]

socket.getaddrinfo = getaddrinfo

def query(url):
    return urllib.urlopen(url).read()

def print_bootstrap_lines(line):
  if "Bootstrapped " in line:
    print(term.format(line, term.Color.BLUE))

print(term.format("Starting Tor:\n", term.Attr.BOLD))

tor_process = stem.process.launch_tor_with_config(
  config = {
    'SocksPort': str(SOCKS_PORT),
    'ExitNodes': '{ru}',
  },
  init_msg_handler = print_bootstrap_lines,
)

print(term.format("\nChecking our endpoint:\n", term.Attr.BOLD))
print(term.format(query("https://www.atagar.com/echo.php"), term.Color.BLUE))

print(query("https://stackoverflow.com/questions/30286293/make-requests-using-python-over-tor"))

tor_process.kill()  

I have seen answers saying that there is another process running on port 9050, but runniing the command sudo netstat -anl | grep 9050 in the terminal gives me no result.

Please feel free to suggest anything that might help.

In addition, the first example on the "to russia with love" works flawlessly.

Thanks for any help that you can give.


Solution

  • check tor is installed

    sudo apt install tor
    

    Check this

    sudo gedit /etc/default/tor
    

    find and replece "yes" to "no" there

    RUN_DAEMON="yes"
    

    stop the daemon

    sudo /etc/init.d/tor stop