python - Stem 出现 "Unable to connect to port 9051"错误

标签 python python-2.7 tor stem

我试过了 this示例:

import getpass
import sys

import stem
import stem.connection

from stem.control import Controller

if __name__ == '__main__':
  try:
    controller = Controller.from_port()
  except stem.SocketError as exc:
    print("Unable to connect to tor on port 9051: %s" % exc)
    sys.exit(1)

  try:
    controller.authenticate()
  except stem.connection.MissingPassword:
    pw = getpass.getpass("Controller password: ")

    try:
      controller.authenticate(password = pw)
    except stem.connection.PasswordAuthFailed:
      print("Unable to authenticate, password is incorrect")
      sys.exit(1)
  except stem.connection.AuthenticationFailure as exc:
    print("Unable to authenticate: %s" % exc)
    sys.exit(1)

  print("Tor is running version %s" % controller.get_version())
  controller.close()

在 Stem 上检查 Tor 的 Controller 如何工作。但它给了我错误:

Unable to connect to port 9051 ([Errno 111] Connection refused)

我已经在/etc/tor/内的 torrc 文件中将 ControlPort 设置为 9051,但仍然出现相同的错误。有人可以帮忙吗? (我在 VirtualBox 上使用 Ubuntu 14.04)

编辑:同时在终端上运行 tor --controlport 9051 会出现错误:

Feb 29 17:50:17.842 [notice] Tor v0.2.4.27 (git-412e3f7dc9c6c01a) running on Linux with Libevent 2.0.21-stable and OpenSSL 1.0.1f.
Feb 29 17:50:17.842 [notice] Tor can't help you if you use it wrong! Learn how to be safe at https://www.torproject.org/download/download#warning
Feb 29 17:50:17.842 [notice] Read configuration file "/etc/tor/torrc".
Feb 29 17:50:17.854 [notice] Opening Socks listener on 127.0.0.1:9050
Feb 29 17:50:17.854 [warn] Could not bind to 127.0.0.1:9050: Address already in use. Is Tor already running?
Feb 29 17:50:17.854 [notice] Opening Control listener on 127.0.0.1:9051
Feb 29 17:50:17.854 [notice] Closing partially-constructed Control listener on 127.0.0.1:9051
Feb 29 17:50:17.854 [warn] Failed to parse/validate config: Failed to bind one of the listener ports.
Feb 29 17:50:17.854 [err] Reading config failed--see warnings above.

但是当我运行的时候

ps

没有运行 tor 的实例。 编辑 2:我尝试了命令 sudo killall tor,然后用 sudo/etc/init.d/tor status 检查了它,现在它给出了一个身份验证错误:

 File "circuitPage.py", line 82, in printCircuitInfo
    controller.authenticate()
  File "/usr/local/lib/python2.7/dist-packages/stem/control.py", line 991, in authenticate
    stem.connection.authenticate(self, *args, **kwargs)
  File "/usr/local/lib/python2.7/dist-packages/stem/connection.py", line 520, in authenticate
    raise AuthenticationFailure('socket connection failed (%s)' % exc)
stem.connection.AuthenticationFailure: socket connection failed ([Errno 111] Connection refused)

最佳答案

/etc/tor/torrc 配置文件中找到 ControlPort 9051 然后取消注释该行。
然后通过以下方式重置 tor:
sudo 服务重启
现在你可以连接到端口 9051

关于python - Stem 出现 "Unable to connect to port 9051"错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35699833/

相关文章:

python - 如何在 Python 中重构 100 多个类方法?

python - 在一个网格布局中设置随机列数

ruby - 如何正确使用 tor-privoxy Ruby gem?

nginx - 如何创建 .onion 网站?

python - rxpy 如何取消订阅

python - 获取 OpenStreetMap map 的最大值和最小值

python - 拆分列中的名称

python-2.7 - 为什么有些操作符在 Odoo 中的记录集没有按预期工作?

python - 更改级别记录到 IPython/Jupyter notebook

python - 在python中获取新的IP