如何清除pySNMP超时?

时间:2012-07-17 15:46:12

标签: python pysnmp

我想检查树状态系统的状态,我正在使用以下代码,但是我遇到了超时问题,例如当系统中的一个系统关闭而不是下一个系统时,我正在等待超时好。

try:
    snmp = pySNMPCom( ip="192.168.0.1", 23434, 30 )
    """It's hangig 30 seconds that is the timeout"""            
    data = snmp.emite_command( command )
    snmp.destroye()                
except:  
    """Smtp timeout"""
    snmp.destroye()  

try:
    snmp = pySNMPCom( ip="192.168.0.2", 23434, 30 )
    """It's going to the exception immediately, it suppose to hung 30 seconds...""" 
    data = snmp.emite_command( command )
    snmp.destroye()                
except:  
    """Smtp timeout"""
    snmp.destroye()  

班级:

# -*- coding: utf-8 -*-
from pysnmp.carrier.asynsock.dispatch import AsynsockDispatcher
from pysnmp.carrier.asynsock.dgram import udp
from pyasn1.codec.ber import encoder, decoder
from pysnmp.proto import api
from time import time


class pySNMPCom():

    def __init__( self, ip, port, timeout ):
        self.__host = ip
        self.__port = port
        self.__timeout = timeout

    def set_cmd( self, cmd ):
        self.cmd = cmd

    def __set_message( self ):
        self.__pMod = api.protoModules[api.protoVersion1]

        self.__reqPDU = self.__pMod.GetRequestPDU()
        self.__pMod.apiPDU.setDefaults( self.__reqPDU )
        self.__pMod.apiPDU.setVarBinds( 
            self.__reqPDU, ( ( self.cmd, self.__pMod.Null( '' ) ),
                )
            )
        # Build message
        self.__reqMsg = self.__pMod.Message()
        self.__pMod.apiMessage.setDefaults( self.__reqMsg )
        self.__pMod.apiMessage.setCommunity( self.__reqMsg, 'public' )
        self.__pMod.apiMessage.setPDU( self.__reqMsg, self.__reqPDU )

    def __cbTimerFun( self, timeNow, startedAt = time() ):
        if timeNow - startedAt > self.__timeout:
            raise "Request timed out"

    def __cbRecvFun( self, transportDispatcher, transportDomain, transportAddress, wholeMsg, reqPDU = '' ):
        if not reqPDU:
            reqPDU = self.__reqPDU
        self.ret = ''
        while wholeMsg:
            rspMsg, wholeMsg = decoder.decode( wholeMsg, asn1Spec = self.__pMod.Message() )
            rspPDU = self.__pMod.apiMessage.getPDU( rspMsg )
            # Match response to request
            if self.__pMod.apiPDU.getRequestID( reqPDU ) == self.__pMod.apiPDU.getRequestID( rspPDU ):
                # Check for SNMP errors reported
                errorStatus = self.__pMod.apiPDU.getErrorStatus( rspPDU )
                if errorStatus:
                    print errorStatus.prettyPrint()
                else:
                    for oid, val in self.__pMod.apiPDU.getVarBinds( rspPDU ):                        
                        self.ret = val

                transportDispatcher.jobFinished( 1 )

    def emite_command( self, cmd = '' ):
        if not cmd:
            cmd = self.cmd
        self.set_cmd( cmd )
        self.__set_message()        

        transportDispatcher = AsynsockDispatcher()
        transportDispatcher.registerTransport( 
            udp.domainName, udp.UdpSocketTransport().openClientMode()
            )
        transportDispatcher.registerRecvCbFun( self.__cbRecvFun )
        transportDispatcher.registerTimerCbFun( self.__cbTimerFun )
        transportDispatcher.sendMessage( 
            encoder.encode( self.__reqMsg ), udp.domainName, ( self.__host, self.__port )
            )
        transportDispatcher.jobStarted( 1 )
        transportDispatcher.runDispatcher()
        transportDispatcher.closeDispatcher()

        return self.ret

    def destroye( self ):
        del self      

1 个答案:

答案 0 :(得分:1)

将initialAt变量初始化为默认方法参数。这只在模块运行时发生一次,从不在类重新实例化时重复。因此,所有查询计时都是根据初始staredAt值来衡量的。因此,所有在beginAt + 30之后发生的查询都会立即超时。