【问题标题】:Python tor stem Events not working as expectedPython tor stem 事件未按预期工作
【发布时间】:2020-05-23 12:16:25
【问题描述】:

我一直在尝试向NavigaTor(用python 2 编写)添加一些功能,它使用stem 库来构建TOR 电路。基本上,如果电路发生故障,我会尝试使用新路径构建一个新路径(默认情况下,Navigator 不会这样做,如果电路发生故障则返回)。 relevant code如下:

Probe = namedtuple('Probe', 'path circs cbt streams perf bw')
def _circuit_handler(event):
    """ Event handler for handling circuit states. """
    if not event.build_flags or 'IS_INTERNAL' not in event.build_flags:
        if event.id == self._cid:
            probe.circs.append(event)
            if self._circuit_built.is_set():
                if event.status in ('FAILED', 'CLOSED'):
                    self._circuit_finished.set()
            if not self._circuit_built.is_set():
                if event.status in ('FAILED', 'BUILT'):
                    self._circuit_built.set()
        elif event.status == 'LAUNCHED' and not self._cid:
            self._cid = event.id
            probe.circs.append(event)
            self._manager.circ_launched.release()

在while循环中,我正在尝试构建电路,直到成功创建电路:

while(true):        #try until a valid circuit is received:
    probe = Probe(path=self.path, circs=[], \
        cbt=set(), streams=[],perf=[], bw=[])
    circ_path = [node.desc.fingerprint for node in self.path]       #Valid nodes already acquired and stored in path

    self._manager.circ_launched.acquire()
    self._controller.add_event_listener(_circuit_handler, EventType.CIRC)
    self._controller.add_event_listener(_cbt_check, EventType.INFO)
    circID = self._controller.extend_circuit(path=circ_path)
    self._circuit_built.wait()

    build_status = probe.circs[len(probe.circs) - 1].status
    assert build_status == 'BUILT' or build_status == 'FAILED', \
        'Wrong circuit status: %s.' % build_statusFirst

    if build_status == 'FAILED':
        self._controller.remove_event_listener(_circuit_handler)
        self._controller.remove_event_listener(_cbt_check)
        self.path = self._manager._get_new_path()
    else:
        break

现在,我的问题是,如果电路在第一次尝试中成功,那么这个东西就可以正常工作。但是如果 build_status 失败,并且循环使用新的有效路径再次运行,代码会在以下行崩溃:

build_status = probe.circs[len(probe.circs) - 1].status

出现 IndexError:列表索引超出范围。在调试时,我观察到 probe.circs 仍然是空的,即使 _circuit_handler 事件应该附加到它上面。对于第二个电路,这种情况每次都会发生,即使如果第一个电路成功,它也可以正常工作。我在这里做错了什么?

【问题讨论】:

    标签: python python-2.7 tor stem


    【解决方案1】:

    想通了,_cid 在电路故障时没有被重置。添加 self._cid = None 来修复。

    【讨论】:

      猜你喜欢
      • 2016-02-04
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多