Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8246637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T22:34:18+00:00 2026-06-07T22:34:18+00:00

I would like to check tree system’s status, I’m using the following code, but

  • 0

I would like to check tree system’s status, I’m using the following code, but I have problem with the timeout, for example when one of the system is down than in the next system I’m getting a time out as well.

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()  

The class:

# -*- 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 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T22:34:21+00:00Added an answer on June 7, 2026 at 10:34 pm

    You initialize the startedAt variable as a defaulted method parameter. That happens only once in a module runtime and never repeats on class re-instantiation. So all your queries timings are measured against the initial staredAt value. Therefore all queries occurring past startedAt + 30 will timeout right away.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I would like to check if an object is defined or exists using C#.
I would like to check that I have workingDir = C:\Users\~\Temp\workspace_folder; for i=1:2 %
I have a simple issue -- I would like to check a field to
I have a dir structure like the following: [me@mypc]$ tree . . ├── set01
I would like to check to see if a DOM Node is inside an
I would like to check if a few URL's exst on my old website
I would like to check whether the request is XML od HTML. When HTML
I would like to check the memory consumption of each running processes individually, cat
I would like to check what colors is present in a image. This will
We would like to check if a specified process is currently running via PHP.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.