向 stem 发送信号关闭套接字

Sending a signal to stem closes the socket

此代码:

from stem import Signal
from stem.control import Controller

with Controller.from_port(port=9051) as controller:
    print('is alive BEFORE ? ')
    print(controller.is_alive())
    try:
        controller.signal(Signal.HEARTBEAT)
    except Exception as e:
        print(e)
    print('is alive AFTER ? ')
    print(controller.is_alive())

with Controller.from_port(port=9051) as controller:
    print('is alive 2 ? ')
    print(controller.is_alive())

产生这个输出:

is alive BEFORE ?
True
SIGNAL response contained unrecognized status code: 514
is alive AFTER ?
False
is alive 2 ?
True

并且没有记录心跳。另外,如果我在发送信号后尝试向 tor 发出请求,我会得到:[stem] INFO: Error while receiving a control message (SocketClosed): empty socket content

Tor 配置为: 袜口 9050 控制端口 9051

514 错误表示 Authentication required

连接后,您要做的第一件事就是进行身份验证:

with Controller.from_port(port=9051) as controller:
    print('is alive BEFORE ? ')
    print(controller.is_alive())
    controller.authenticate()
    ...

另请注意,Tor 会在身份验证失败后关闭连接。有关详细信息,请参阅 control spec 第 3.5 节。

第一个词干示例更详细地介绍了这些基础知识:https://stem.torproject.org/tutorials/the_little_relay_that_could.html