簡體   English   中英

訪問已接收的TRAP的varBinds

[英]Access to varBinds of received TRAP

我沒有從代碼示例中獲取如何訪問varBinds的信息。 我可以打印它,但是如果我要存儲它或將其傳遞給方法或類怎么辦?

即使將其存儲在類中,也無法進一步訪問它。


class TrapReceiver
    def __init__(self):
        # Create SNMP engine with autogenernated engineID and pre-bound
        # to socket transport dispatcher
        self.snmpEngine = engine.SnmpEngine()
        # Transport setup
        # UDP over IPv4, first listening interface/port
        config.addTransport(
            self.snmpEngine,
            udp.domainName + (1,),
            udp.UdpTransport().openServerMode(('127.0.0.1', 162))
        )
        # SNMPv1/2c setup
        # SecurityName <-> CommunityName mapping
        config.addV1System(self.snmpEngine, 'my-area', 'public')
    # Callback function for receiving notifications
    # noinspection PyUnusedLocal,PyUnusedLocal,PyUnusedLocal
    def cbFun(self,snmpEngine, stateReference, contextEngineId, contextName,
                  varBinds, cbCtx):
            for varBind in varBinds:
                oid, value = varBind
                trap_source = str(oid)
                trap_val = int(value)
                #TODO: return trap_source, trap_val

    def run(self):
        # Register SNMP Application at the SNMP engine
        ntfrcv.NotificationReceiver(self.snmpEngine, self.cbFun)

        self.snmpEngine.transportDispatcher.jobStarted(1)  # this job would never finish

        # Run I/O dispatcher which would receive queries and send confirmations
        try:
            self.snmpEngine.transportDispatcher.runDispatcher()
        except:
            self.snmpEngine.transportDispatcher.closeDispatcher()
            raise

您不能從回調函數“返回”任何東西。 因為其調用方(主循環)對其返回值不感興趣。

因此,您應該使用cbCtx或其他一些全局對象(例如dict )將在回調函數中接收的信息傳遞給應用程序的其他部分。 可以cbCtx對象傳遞給NotificationReceiver ,然后將其顯示在回調函數中。

因此,例如,您可以在一個線程中具有通知接收器,該線程將接收到的數據推入cbCtx數據結構(例如,可以是Queue ),然后另一個線程從該Queue彈出項目並進行處理。

或者,您可以直接在回調函數中處理接收到的數據。 只要確保它沒有阻塞即可。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM