0

私は、Stem ライブラリを使用して TOR 回路を構築するNavigaTor (python 2 で記述)にいくつかの機能を追加しようとしています。基本的に、回線が失敗した場合、新しいパスを使用して新しい回線を構築しようとしています (ナビゲーターはデフォルトではそれを行わず、回線が失敗した場合に戻ります)。関連するコードは次のとおりです。

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 が FAILED で、ループが新しい有効なパスで再度実行されると、コードは次の行でクラッシュします。

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

IndexError の場合: リスト インデックスが範囲外です。デバッグ時に、_circuit_handler イベントが追加されるはずなのに、probe.circs が空のままであることを確認しました。これは、最初の回線が成功した場合でも正常に動作するにもかかわらず、2 番目の回線で毎回発生します。ここで何が間違っていますか?

4

1 に答える 1