text: #!/usr/bin/env python3
  # -*- coding: utf-8 -*-
  #
  #
  # Copyright © 2018 Dell Inc. or its subsidiaries. All rights reserved.
  # Dell, EMC, and other trademarks are trademarks of Dell Inc. or its subsidiaries.
  # Other trademarks may be trademarks of their respective owners.
  #
  # Licensed under the Apache License, Version 2.0 (the "License");
  # you may not use this file except in compliance with the License.
  # You may obtain a copy of the License at
  #
  #    http://www.apache.org/licenses/LICENSE-2.0
  #
  # Unless required by applicable law or agreed to in writing, software
  # distributed under the License is distributed on an "AS IS" BASIS,
  # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  # See the License for the specific language governing permissions and
  # limitations under the License.
  #
  # Authors: Vaideeswaran Ganesan
  #
  from pysnmp.carrier.asynsock.dispatch import AsynsockDispatcher
  from pysnmp.carrier.asynsock.dgram import udp, udp6
  from pyasn1.codec.ber import decoder
  from pysnmp.proto import api
  import logging
  
  logger = logging.getLogger(__name__)
  
  
  def cbFun(transportDispatcher, transportDomain, transportAddress, wholeMsg):
      while wholeMsg:
          msgVer = int(api.decodeMessageVersion(wholeMsg))
          if msgVer in api.protoModules:
              pMod = api.protoModules[msgVer]
          else:
              logger.debug('Unsupported SNMP version %s' % msgVer)
              return
          reqMsg, wholeMsg = decoder.decode(
              wholeMsg, asn1Spec=pMod.Message(),
          )
          logger.debug('Notification message from %s:%s: ' % (
              transportDomain, transportAddress
          )
                       )
          reqPDU = pMod.apiMessage.getPDU(reqMsg)
          if reqPDU.isSameTypeWith(pMod.TrapPDU()):
              if msgVer == api.protoVersion1:
                  logger.debug('Enterprise: %s' % (
                      pMod.apiTrapPDU.getEnterprise(reqPDU).prettyPrint()
                  )
                               )
                  logger.debug('Agent Address: %s' % (
                      pMod.apiTrapPDU.getAgentAddr(reqPDU).prettyPrint()
                  )
                               )
                  logger.debug('Generic Trap: %s' % (
                      pMod.apiTrapPDU.getGenericTrap(reqPDU).prettyPrint()
                  )
                               )
                  logger.debug('Specific Trap: %s' % (
                      pMod.apiTrapPDU.getSpecificTrap(reqPDU).prettyPrint()
                  )
                               )
                  logger.debug('Uptime: %s' % (
                      pMod.apiTrapPDU.getTimeStamp(reqPDU).prettyPrint()
                  )
                               )
                  varBinds = pMod.apiTrapPDU.getVarBindList(reqPDU)
              else:
                  varBinds = pMod.apiPDU.getVarBindList(reqPDU)
              logger.debug('Var-binds:')
              for oid, val in varBinds:
                  logger.debug('%s = %s' % (oid.prettyPrint(), val.prettyPrint()))
      return wholeMsg
  
  
  transportDispatcher = AsynsockDispatcher()
  
  transportDispatcher.registerRecvCbFun(cbFun)
  
  # UDP/IPv4
  transportDispatcher.registerTransport(
      udp.domainName, udp.UdpSocketTransport().openServerMode(('localhost', 162))
  )
  
  # UDP/IPv6
  transportDispatcher.registerTransport(
      udp6.domainName, udp6.Udp6SocketTransport().openServerMode(('::1', 162))
  )
  
  transportDispatcher.jobStarted(1)
  
  try:
      # Dispatcher will never finish as job#1 never reaches zero
      transportDispatcher.runDispatcher()
  except:
      transportDispatcher.closeDispatcher()
      raise
  