如何通过Python和Stem在Linux上获取Tor relay信息?

1 投票
2 回答
1714 浏览
提问于 2025-04-18 00:34

我正在尝试在Ubuntu Linux上使用Python的stem库来控制Tor,按照Tor的网站上的说明进行操作。但是,当我运行建议的Python代码时,

from stem.control import Controller

with Controller.from_port(port = 9051) as controller:
    controller.authenticate()  # provide the password here if you set one

    bytes_read = controller.get_info("traffic/read")

    bytes_written = controller.get_info("traffic/written")

    print "My Tor relay has read %s bytes and written %s." % (bytes_read, bytes_written)

我遇到了一个错误:

追踪记录(最近的调用在最前面):

文件 "littleRelay.py",第5行,在模块中

bytes_read = controller.get_info("traffic/read")

文件 "/usr/local/lib/python2.7/dist-packages/stem/control.py",第852行,在get_info中

引发异常

stem.InvalidArguments: GETINFO请求包含无法识别的关键字:traffic/read

那么,我该如何通过Python和stem在Linux上获取Tor中继的信息呢?我觉得Tor运行得很好,因为我从终端启动了Tor,它显示:

[通知] Tor成功打开了一个电路。看起来客户端功能正常。

[通知] 启动完成100%:完成。

此外,当我运行上面的Python代码时,终端显示:

[通知] 新的控制连接已打开。

附言:我还在一台Windows电脑上尝试了这段代码,并且成功了。现在我真的很困惑。

2 个回答

0

问题解决了!谢谢你,Damian!

我在Ubuntu上卸载了Tor,然后按照这个详细的指南重新安装了Tor。现在Tor可以和我的Python代码一起正常工作了。

我不太确定问题到底是怎么出现的,但我猜可能是因为我之前在Ubuntu上安装Tor时,直接用这个命令:

sudo apt-get install tor

3

这个错误提示说明Tor不支持'GETINFO traffic/read'这个查询。这有点奇怪,因为这个功能我在2011年就加到了Tor里。也许你用的Tor版本非常非常旧了?

撰写回答