【问题标题】:Tor failing to run with Failed to bind one of the listener portsTor 无法运行,无法绑定侦听器端口之一
【发布时间】:2018-03-24 21:44:22
【问题描述】:

当我从 stem 文档中运行 [example][1] 时,我得到了错误:

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

我正在运行的确切代码如下:

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

我看到答案说在端口 9050 上运行了另一个进程,但是在终端中运行命令 sudo netstat -anl | grep 9050 没有结果。

请随时提出任何可能有帮助的建议。

此外,“用爱去俄罗斯”的第一个例子完美无缺。

感谢您提供的任何帮助。

【问题讨论】:

    标签: python tor stem


    【解决方案1】:

    检查tor是否安装

    sudo apt install tor
    

    检查一下

    sudo gedit /etc/default/tor
    

    在那里找到并用“是”替换为“否”

    RUN_DAEMON="yes"
    

    停止守护进程

    sudo /etc/init.d/tor stop
    

    【讨论】:

      【解决方案2】:

      由于您使用 SOCKS_PORT 作为 7000,请使用以下命令查找使用它的进程。

      sudo netstat -plnt | grep 7000
      

      如果你得到类似这样的输出(见最后的 tor),

      tcp        0      0 127.0.0.1:7000          0.0.0.0:*               LISTEN      13009/tor
      

      使用sudo killall tor 终止tor 进程。

      不推荐使用“sudo”和进程名称(“tor”)来终止命令。相反,您应该使用kill pid 命令,其中 pid 是上述输出中的进程 ID (13009)。

      【讨论】:

        【解决方案3】:

        我在使用“线程”包时遇到了这个问题,这是我的解决方案,可能对流浪者很方便。

        我使用了最佳答案提供的解决方案 并且还这样做了:

        for thread in threads_list:
            time.sleep(0.1)
            thread.run()
        

        【讨论】:

          【解决方案4】:

          在 Linux Mint 上工作并遇到了这个问题。

          能够通过长时间睡眠/暂停来绕过它。

          import time
          time.sleep(1000)      #usually sleep it over 10 minutes for my case
          

          从我正在抓取的内容来看,它需要睡觉,所以它是完美的,但我当然可以看到它在其他情况下没有帮助。

          【讨论】:

            猜你喜欢
            • 2020-02-13
            • 2019-05-05
            • 1970-01-01
            • 2020-05-01
            • 2022-07-01
            • 1970-01-01
            • 2021-05-28
            • 2021-12-22
            • 2014-10-13
            相关资源
            最近更新 更多