向阀杆发送信号会关闭插座

时间:2018-06-02 14:41:03

标签: python networking tor stem

此代码:

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

并且未记录任何心跳。此外,如果我在发送信号后尝试发出请求,我得到:[stem] INFO:接收控制消息时出错(SocketClosed):空套接字内容

Tor配置是: SocksPort 9050 ControlPort 9051

1 个答案:

答案 0 :(得分:0)

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