From 90bbf397ad3dd49db7f83d541afff51f17e63054 Mon Sep 17 00:00:00 2001 From: Ilya Etingof Date: Sat, 2 Apr 2016 23:43:14 +0200 Subject: pep8 reformatted --- pysnmp/carrier/asyncio/base.py | 1 + pysnmp/carrier/asyncio/dgram/base.py | 8 ++++++-- pysnmp/carrier/asyncio/dgram/udp.py | 4 ++++ pysnmp/carrier/asyncio/dgram/udp6.py | 6 +++++- pysnmp/carrier/asyncio/dispatch.py | 9 ++++++--- pysnmp/carrier/asyncore/base.py | 10 ++++++---- pysnmp/carrier/asyncore/dgram/base.py | 30 ++++++++++++++++++------------ pysnmp/carrier/asyncore/dgram/udp.py | 3 +++ pysnmp/carrier/asyncore/dgram/udp6.py | 5 ++++- pysnmp/carrier/asyncore/dgram/unix.py | 11 ++++++++--- pysnmp/carrier/asyncore/dispatch.py | 3 ++- pysnmp/carrier/base.py | 31 +++++++++++++++++-------------- pysnmp/carrier/error.py | 1 + pysnmp/carrier/sockfix.py | 8 ++++---- pysnmp/carrier/twisted/base.py | 5 ++++- pysnmp/carrier/twisted/dgram/base.py | 1 + pysnmp/carrier/twisted/dgram/udp.py | 12 +++++++++--- pysnmp/carrier/twisted/dgram/unix.py | 9 ++++++--- pysnmp/carrier/twisted/dispatch.py | 2 ++ 19 files changed, 107 insertions(+), 52 deletions(-) (limited to 'pysnmp/carrier') diff --git a/pysnmp/carrier/asyncio/base.py b/pysnmp/carrier/asyncio/base.py index 002239bb..217092a6 100644 --- a/pysnmp/carrier/asyncio/base.py +++ b/pysnmp/carrier/asyncio/base.py @@ -33,6 +33,7 @@ from pysnmp.carrier.asyncio.dispatch import AsyncioDispatcher from pysnmp.carrier.base import AbstractTransport + class AbstractAsyncioTransport(AbstractTransport): protoTransportDispatcher = AsyncioDispatcher """Base Asyncio Transport, to be used with AsyncioDispatcher""" diff --git a/pysnmp/carrier/asyncio/dgram/base.py b/pysnmp/carrier/asyncio/dgram/base.py index 5c3b78b6..3abcc3a3 100644 --- a/pysnmp/carrier/asyncio/dgram/base.py +++ b/pysnmp/carrier/asyncio/dgram/base.py @@ -35,6 +35,7 @@ import traceback from pysnmp.carrier.asyncio.base import AbstractAsyncioTransport from pysnmp.carrier import error from pysnmp import debug + try: import asyncio except ImportError: @@ -42,14 +43,16 @@ except ImportError: loop = asyncio.get_event_loop() + class DgramAsyncioProtocol(asyncio.DatagramProtocol, AbstractAsyncioTransport): """Base Asyncio datagram Transport, to be used with AsyncioDispatcher""" sockFamily = None addressType = lambda x: x transport = None - def __init__(self, *args, **kwargs): + def __init__(self, sock=None, sockMap=None): self._writeQ = [] + self._lport = None def datagram_received(self, datagram, transportAddress): if self._cbFun is None: @@ -95,7 +98,8 @@ class DgramAsyncioProtocol(asyncio.DatagramProtocol, AbstractAsyncioTransport): return self def closeTransport(self): - self._lport.cancel() + if self._lport is not None: + self._lport.cancel() if self.transport is not None: self.transport.close() AbstractAsyncioTransport.closeTransport(self) diff --git a/pysnmp/carrier/asyncio/dgram/udp.py b/pysnmp/carrier/asyncio/dgram/udp.py index aa73fb10..f80de36b 100644 --- a/pysnmp/carrier/asyncio/dgram/udp.py +++ b/pysnmp/carrier/asyncio/dgram/udp.py @@ -33,6 +33,7 @@ import socket from pysnmp.carrier.base import AbstractTransportAddress from pysnmp.carrier.asyncio.dgram.base import DgramAsyncioProtocol + try: import asyncio except ImportError: @@ -42,11 +43,14 @@ loop = asyncio.get_event_loop() domainName = snmpUDPDomain = (1, 3, 6, 1, 6, 1, 1) + class UdpTransportAddress(tuple, AbstractTransportAddress): pass + class UdpAsyncioTransport(DgramAsyncioProtocol): sockFamily = socket.AF_INET addressType = UdpTransportAddress + UdpTransport = UdpAsyncioTransport diff --git a/pysnmp/carrier/asyncio/dgram/udp6.py b/pysnmp/carrier/asyncio/dgram/udp6.py index ad3c28dd..290c03bd 100644 --- a/pysnmp/carrier/asyncio/dgram/udp6.py +++ b/pysnmp/carrier/asyncio/dgram/udp6.py @@ -7,6 +7,7 @@ import socket from pysnmp.carrier.base import AbstractTransportAddress from pysnmp.carrier.asyncio.dgram.base import DgramAsyncioProtocol + try: import asyncio except ImportError: @@ -16,9 +17,11 @@ loop = asyncio.get_event_loop() domainName = snmpUDP6Domain = (1, 3, 6, 1, 2, 1, 100, 1, 2) + class Udp6TransportAddress(tuple, AbstractTransportAddress): pass + class Udp6AsyncioTransport(DgramAsyncioProtocol): sockFamily = socket.has_ipv6 and socket.AF_INET6 or None addressType = Udp6TransportAddress @@ -28,9 +31,10 @@ class Udp6AsyncioTransport(DgramAsyncioProtocol): return self.addressType((transportAddress[0].split('%')[0], transportAddress[1], 0, # flowinfo - 0)) # scopeid + 0)) # scopeid else: return self.addressType((transportAddress[0], transportAddress[1], 0, 0)) + Udp6Transport = Udp6AsyncioTransport diff --git a/pysnmp/carrier/asyncio/dispatch.py b/pysnmp/carrier/asyncio/dispatch.py index 0cfa4ceb..cb4c0941 100644 --- a/pysnmp/carrier/asyncio/dispatch.py +++ b/pysnmp/carrier/asyncio/dispatch.py @@ -34,6 +34,7 @@ import sys import traceback from pysnmp.carrier.base import AbstractTransportDispatcher from pysnmp.error import PySnmpError + try: import asyncio except ImportError: @@ -41,8 +42,10 @@ except ImportError: loop = asyncio.get_event_loop() + class AsyncioDispatcher(AbstractTransportDispatcher): """AsyncioDispatcher based on asyncio event loop""" + def __init__(self, *args, **kwargs): AbstractTransportDispatcher.__init__(self) self.__transportCount = 0 @@ -70,7 +73,7 @@ class AsyncioDispatcher(AbstractTransportDispatcher): self.loopingcall = asyncio.async(self.handle_timeout()) AbstractTransportDispatcher.registerTransport( self, tDomain, transport - ) + ) self.__transportCount += 1 def unregisterTransport(self, tDomain): @@ -84,9 +87,10 @@ class AsyncioDispatcher(AbstractTransportDispatcher): self.loopingcall.cancel() self.loopingcall = None + # Trollius or Tulip? if not hasattr(asyncio, "From"): - exec("""\ + exec ("""\ @asyncio.coroutine def handle_timeout(self): while True: @@ -94,4 +98,3 @@ def handle_timeout(self): self.handleTimerTick(loop.time()) AsyncioDispatcher.handle_timeout = handle_timeout\ """) - diff --git a/pysnmp/carrier/asyncore/base.py b/pysnmp/carrier/asyncore/base.py index da68774d..04044775 100644 --- a/pysnmp/carrier/asyncore/base.py +++ b/pysnmp/carrier/asyncore/base.py @@ -4,13 +4,15 @@ # Copyright (c) 2005-2016, Ilya Etingof # License: http://pysnmp.sf.net/license.html # -import socket, sys +import socket +import sys import asyncore from pysnmp.carrier import error from pysnmp.carrier.base import AbstractTransport from pysnmp.carrier.asyncore.dispatch import AsyncoreDispatcher from pysnmp import debug + class AbstractSocketTransport(asyncore.dispatcher, AbstractTransport): protoTransportDispatcher = AsyncoreDispatcher sockFamily = sockType = None @@ -25,11 +27,11 @@ class AbstractSocketTransport(asyncore.dispatcher, AbstractTransport): if self.sockFamily is None: raise error.CarrierError( 'Address family %s not supported' % self.__class__.__name__ - ) + ) if self.sockType is None: raise error.CarrierError( 'Socket type %s not supported' % self.__class__.__name__ - ) + ) try: sock = socket.socket(self.sockFamily, self.sockType) except socket.error: @@ -42,7 +44,7 @@ class AbstractSocketTransport(asyncore.dispatcher, AbstractTransport): sock.setsockopt(socket.SOL_SOCKET, b, self.bufferSize) debug.logger & debug.flagIO and debug.logger('%s: socket %d buffer size increased from %d to %d for buffer %d' % (self.__class__.__name__, sock.fileno(), bsize, self.bufferSize, b)) except Exception: - debug.logger & debug.flagIO and debug.logger('%s: socket buffer size option mangling failure for buffer %d: %s' % (self.__class__.__name__, b, sys.exc_info()[1])) + debug.logger & debug.flagIO and debug.logger('%s: socket buffer size option mangling failure for buffer: %s' % (self.__class__.__name__, sys.exc_info()[1])) # The socket map is managed by the AsyncoreDispatcher on # which this transport is registered. Here we just prepare diff --git a/pysnmp/carrier/asyncore/dgram/base.py b/pysnmp/carrier/asyncore/dgram/base.py index c64b7e89..49b7846e 100644 --- a/pysnmp/carrier/asyncore/dgram/base.py +++ b/pysnmp/carrier/asyncore/dgram/base.py @@ -4,7 +4,9 @@ # Copyright (c) 2005-2016, Ilya Etingof # License: http://pysnmp.sf.net/license.html # -import socket, errno, sys +import socket +import errno +import sys from pysnmp.carrier.asyncore.base import AbstractSocketTransport from pysnmp.carrier import sockfix, sockmsg, error from pysnmp import debug @@ -21,17 +23,21 @@ if hasattr(errno, 'EBADFD'): # bad FD may happen upon FD closure on n-1 select() event sockErrors[errno.EBADFD] = True + class DgramSocketTransport(AbstractSocketTransport): sockType = socket.SOCK_DGRAM retryCount = 3 retryInterval = 1 addressType = lambda x: x + def __init__(self, sock=None, sockMap=None): self.__outQueue = [] self._sendto = lambda s, b, a: s.sendto(b, a) + def __recvfrom(s, sz): d, a = s.recvfrom(sz) return d, self.addressType(a) + self._recvfrom = __recvfrom AbstractSocketTransport.__init__(self, sock, sockMap) @@ -40,10 +46,11 @@ class DgramSocketTransport(AbstractSocketTransport): try: self.socket.bind(iface) except socket.error: - raise error.CarrierError('bind() for %s failed: %s' % (iface is None and "" or iface, sys.exc_info()[1])) + raise error.CarrierError( + 'bind() for %s failed: %s' % (iface is None and "" or iface, sys.exc_info()[1])) return self - def openServerMode(self, iface=None): + def openServerMode(self, iface): try: self.socket.bind(iface) except socket.error: @@ -62,7 +69,7 @@ class DgramSocketTransport(AbstractSocketTransport): def enablePktInfo(self, flag=1): if not hasattr(self.socket, 'sendmsg') or \ - not hasattr(self.socket, 'recvmsg'): + not hasattr(self.socket, 'recvmsg'): raise error.CarrierError('sendmsg()/recvmsg() interface is not supported by this OS and/or Python version') try: @@ -100,7 +107,7 @@ class DgramSocketTransport(AbstractSocketTransport): def sendMessage(self, outgoingMessage, transportAddress): self.__outQueue.append( (outgoingMessage, self.normalizeAddress(transportAddress)) - ) + ) debug.logger & debug.flagIO and debug.logger('sendMessage: outgoingMessage queued (%d octets) %s' % (len(outgoingMessage), debug.hexdump(outgoingMessage))) def normalizeAddress(self, transportAddress): @@ -114,8 +121,8 @@ class DgramSocketTransport(AbstractSocketTransport): # one evil OS does not seem to support getsockname() for DGRAM sockets try: return self.socket.getsockname() - except: - return ('0.0.0.0', 0) + except Exception: + return '0.0.0.0', 0 # asyncore API def handle_connect(self): @@ -145,11 +152,10 @@ class DgramSocketTransport(AbstractSocketTransport): def handle_read(self): try: - incomingMessage, transportAddress = self._recvfrom( - self.socket, 65535 - ) + incomingMessage, transportAddress = self._recvfrom(self.socket, 65535) transportAddress = self.normalizeAddress(transportAddress) - debug.logger & debug.flagIO and debug.logger('handle_read: transportAddress %r -> %r incomingMessage (%d octets) %s' % (transportAddress, transportAddress.getLocalAddress(), len(incomingMessage), debug.hexdump(incomingMessage))) + debug.logger & debug.flagIO and debug.logger( + 'handle_read: transportAddress %r -> %r incomingMessage (%d octets) %s' % (transportAddress, transportAddress.getLocalAddress(), len(incomingMessage), debug.hexdump(incomingMessage))) if not incomingMessage: self.handle_close() return @@ -165,4 +171,4 @@ class DgramSocketTransport(AbstractSocketTransport): raise error.CarrierError('recvfrom() failed: %s' % (sys.exc_info()[1],)) def handle_close(self): - pass # no datagram connection + pass # no datagram connection diff --git a/pysnmp/carrier/asyncore/dgram/udp.py b/pysnmp/carrier/asyncore/dgram/udp.py index f8b5ac1b..09b783dd 100644 --- a/pysnmp/carrier/asyncore/dgram/udp.py +++ b/pysnmp/carrier/asyncore/dgram/udp.py @@ -10,11 +10,14 @@ from pysnmp.carrier.asyncore.dgram.base import DgramSocketTransport domainName = snmpUDPDomain = (1, 3, 6, 1, 6, 1, 1) + class UdpTransportAddress(tuple, AbstractTransportAddress): pass + class UdpSocketTransport(DgramSocketTransport): sockFamily = AF_INET addressType = UdpTransportAddress + UdpTransport = UdpSocketTransport diff --git a/pysnmp/carrier/asyncore/dgram/udp6.py b/pysnmp/carrier/asyncore/dgram/udp6.py index 947ecd23..c659dc35 100644 --- a/pysnmp/carrier/asyncore/dgram/udp6.py +++ b/pysnmp/carrier/asyncore/dgram/udp6.py @@ -11,9 +11,11 @@ import socket domainName = snmpUDP6Domain = (1, 3, 6, 1, 2, 1, 100, 1, 2) + class Udp6TransportAddress(tuple, AbstractTransportAddress): pass + class Udp6SocketTransport(DgramSocketTransport): sockFamily = socket.has_ipv6 and socket.AF_INET6 or None addressType = Udp6TransportAddress @@ -22,7 +24,7 @@ class Udp6SocketTransport(DgramSocketTransport): if '%' in transportAddress[0]: # strip zone ID ta = self.addressType((transportAddress[0].split('%')[0], transportAddress[1], - 0, # flowinfo + 0, # flowinfo 0)) # scopeid else: ta = self.addressType((transportAddress[0], @@ -34,4 +36,5 @@ class Udp6SocketTransport(DgramSocketTransport): else: return ta.setLocalAddress(self.getLocalAddress()) + Udp6Transport = Udp6SocketTransport diff --git a/pysnmp/carrier/asyncore/dgram/unix.py b/pysnmp/carrier/asyncore/dgram/unix.py index f8b193eb..7bac79af 100644 --- a/pysnmp/carrier/asyncore/dgram/unix.py +++ b/pysnmp/carrier/asyncore/dgram/unix.py @@ -6,6 +6,7 @@ # import os import random + try: from socket import AF_UNIX except ImportError: @@ -17,12 +18,15 @@ domainName = snmpLocalDomain = (1, 3, 6, 1, 2, 1, 100, 1, 13) random.seed() + class UnixTransportAddress(str, AbstractTransportAddress): pass + class UnixSocketTransport(DgramSocketTransport): sockFamily = AF_UNIX addressType = UnixTransportAddress + _iface = '' def openClientMode(self, iface=None): if iface is None: @@ -35,21 +39,22 @@ class UnixSocketTransport(DgramSocketTransport): if os.path.exists(iface): os.remove(iface) DgramSocketTransport.openClientMode(self, iface) - self.__iface = iface + self._iface = iface return self def openServerMode(self, iface): DgramSocketTransport.openServerMode(self, iface) - self.__iface = iface + self._iface = iface return self def closeTransport(self): DgramSocketTransport.closeTransport(self) try: - os.remove(self.__iface) + os.remove(self._iface) except OSError: pass + UnixTransport = UnixSocketTransport # Compatibility stub diff --git a/pysnmp/carrier/asyncore/dispatch.py b/pysnmp/carrier/asyncore/dispatch.py index ae602549..78f6dcf8 100644 --- a/pysnmp/carrier/asyncore/dispatch.py +++ b/pysnmp/carrier/asyncore/dispatch.py @@ -12,9 +12,10 @@ from asyncore import loop from pysnmp.carrier.base import AbstractTransportDispatcher from pysnmp.error import PySnmpError + class AsyncoreDispatcher(AbstractTransportDispatcher): def __init__(self): - self.__sockMap = {} # use own map for MT safety + self.__sockMap = {} # use own map for MT safety self.timeout = 0.5 AbstractTransportDispatcher.__init__(self) diff --git a/pysnmp/carrier/base.py b/pysnmp/carrier/base.py index 3aa2de4e..653ea3fe 100644 --- a/pysnmp/carrier/base.py +++ b/pysnmp/carrier/base.py @@ -6,6 +6,7 @@ # from pysnmp.carrier import error + class TimerCallable: def __init__(self, cbFun, callInterval): self.__cbFun = cbFun @@ -35,6 +36,7 @@ class TimerCallable: def __ge__(self, cbFun): return self.__cbFun >= cbFun + class AbstractTransportDispatcher: def __init__(self): self.__transports = {} @@ -54,7 +56,7 @@ class AbstractTransportDispatcher: else: raise error.CarrierError( 'Unregistered transport %s' % (incomingTransport,) - ) + ) if self.__routingCbFun: recvId = self.__routingCbFun( @@ -69,7 +71,7 @@ class AbstractTransportDispatcher: ) else: raise error.CarrierError( - 'No callback for "%r" found - loosing incoming event'%(recvId,) + 'No callback for "%r" found - loosing incoming event' % (recvId,) ) # Dispatcher API @@ -111,7 +113,7 @@ class AbstractTransportDispatcher: if tDomain in self.__transports: raise error.CarrierError( 'Transport %s already registered' % (tDomain,) - ) + ) transport.registerCbFun(self._cbFun) self.__transports[tDomain] = transport self.__transportDomainMap[transport] = tDomain @@ -120,7 +122,7 @@ class AbstractTransportDispatcher: if tDomain not in self.__transports: raise error.CarrierError( 'Transport %s not registered' % (tDomain,) - ) + ) self.__transports[tDomain].unregisterCbFun() del self.__transportDomainMap[self.__transports[tDomain]] del self.__transports[tDomain] @@ -130,18 +132,18 @@ class AbstractTransportDispatcher: return self.__transports[transportDomain] raise error.CarrierError( 'Transport %s not registered' % (transportDomain,) - ) + ) def sendMessage(self, outgoingMessage, transportDomain, transportAddress): if transportDomain in self.__transports: self.__transports[transportDomain].sendMessage( outgoingMessage, transportAddress - ) + ) else: raise error.CarrierError( 'No suitable transport domain for %s' % (transportDomain,) - ) + ) def getTimerResolution(self): return self.__timerResolution @@ -156,7 +158,7 @@ class AbstractTransportDispatcher: return self.__ticks def handleTimerTick(self, timeNow): - if self.__nextTime == 0: # initial initialization + if self.__nextTime == 0: # initial initialization self.__nextTime = timeNow + self.__timerResolution - self.__timerDelta if self.__nextTime >= timeNow: @@ -180,10 +182,7 @@ class AbstractTransportDispatcher: del self.__jobs[jobId] def jobsArePending(self): - if self.__jobs: - return 1 - else: - return 0 + return bool(self.__jobs) def runDispatcher(self, timeout=0.0): raise error.CarrierError('Method not implemented') @@ -196,8 +195,10 @@ class AbstractTransportDispatcher: self.unregisterRecvCbFun() self.unregisterTimerCbFun() + class AbstractTransportAddress: _localAddress = None + def setLocalAddress(self, s): self._localAddress = s return self @@ -208,10 +209,12 @@ class AbstractTransportAddress: def clone(self, localAddress=None): return self.__class__(self).setLocalAddress(localAddress is None and self.getLocalAddress() or localAddress) + class AbstractTransport: protoTransportDispatcher = None addressType = AbstractTransportAddress _cbFun = None + @classmethod def isCompatibleWithDispatcher(cls, transportDispatcher): return isinstance(transportDispatcher, cls.protoTransportDispatcher) @@ -220,7 +223,7 @@ class AbstractTransport: if self._cbFun: raise error.CarrierError( 'Callback function %s already registered at %s' % (self._cbFun, self) - ) + ) self._cbFun = cbFun def unregisterCbFun(self): @@ -234,7 +237,7 @@ class AbstractTransport: def openClientMode(self, iface=None): raise error.CarrierError('Method not implemented') - def openServerMode(self, iface=None): + def openServerMode(self, iface): raise error.CarrierError('Method not implemented') def sendMessage(self, outgoingMessage, transportAddress): diff --git a/pysnmp/carrier/error.py b/pysnmp/carrier/error.py index cdc3b308..290eb659 100644 --- a/pysnmp/carrier/error.py +++ b/pysnmp/carrier/error.py @@ -6,5 +6,6 @@ # from pysnmp import error + class CarrierError(error.PySnmpError): pass diff --git a/pysnmp/carrier/sockfix.py b/pysnmp/carrier/sockfix.py index 3abcd932..4c84321a 100644 --- a/pysnmp/carrier/sockfix.py +++ b/pysnmp/carrier/sockfix.py @@ -7,11 +7,11 @@ import socket symbols = { - 'IP_PKTINFO': 8, - 'IP_TRANSPARENT': 19, - 'SOL_IPV6': 41, + 'IP_PKTINFO': 8, + 'IP_TRANSPARENT': 19, + 'SOL_IPV6': 41, 'IPV6_RECVPKTINFO': 49, - 'IPV6_PKTINFO': 50 + 'IPV6_PKTINFO': 50 } for symbol in symbols: diff --git a/pysnmp/carrier/twisted/base.py b/pysnmp/carrier/twisted/base.py index 60f300ef..590717df 100644 --- a/pysnmp/carrier/twisted/base.py +++ b/pysnmp/carrier/twisted/base.py @@ -15,7 +15,10 @@ from pysnmp.carrier.twisted.dispatch import TwistedDispatcher from pysnmp.carrier.base import AbstractTransport + class AbstractTwistedTransport(AbstractTransport): protoTransportDispatcher = TwistedDispatcher - def __init__(self): + + def __init__(self, sock=None, sockMap=None): self._writeQ = [] + DgramTwistedTransport.__init__(self) diff --git a/pysnmp/carrier/twisted/dgram/base.py b/pysnmp/carrier/twisted/dgram/base.py index 4c556a4a..9dd2334b 100644 --- a/pysnmp/carrier/twisted/dgram/base.py +++ b/pysnmp/carrier/twisted/dgram/base.py @@ -11,6 +11,7 @@ from pysnmp.carrier.twisted.base import AbstractTwistedTransport from pysnmp.carrier import error from pysnmp import debug + class DgramTwistedTransport(DatagramProtocol, AbstractTwistedTransport): """Base Twisted datagram Transport, to be used with TwistedDispatcher""" diff --git a/pysnmp/carrier/twisted/dgram/udp.py b/pysnmp/carrier/twisted/dgram/udp.py index 658359f6..3bf06413 100644 --- a/pysnmp/carrier/twisted/dgram/udp.py +++ b/pysnmp/carrier/twisted/dgram/udp.py @@ -12,11 +12,14 @@ from pysnmp.carrier import error domainName = snmpUDPDomain = (1, 3, 6, 1, 6, 1, 1) + class UdpTransportAddress(tuple, AbstractTransportAddress): pass + class UdpTwistedTransport(DgramTwistedTransport): addressType = UdpTransportAddress + _lport = None # AbstractTwistedTransport API @@ -37,8 +40,11 @@ class UdpTwistedTransport(DgramTwistedTransport): return self def closeTransport(self): - d = self._lport.stopListening() - d and d.addCallback(lambda x: None) - DgramTwistedTransport.closeTransport(self) + if self._lport is not None: + d = self._lport.stopListening() + if d: + d.addCallback(lambda x: None) + DgramTwistedTransport.closeTransport(self) + UdpTransport = UdpTwistedTransport diff --git a/pysnmp/carrier/twisted/dgram/unix.py b/pysnmp/carrier/twisted/dgram/unix.py index 5dc84bd3..61ccff93 100644 --- a/pysnmp/carrier/twisted/dgram/unix.py +++ b/pysnmp/carrier/twisted/dgram/unix.py @@ -17,6 +17,7 @@ class UnixTransportAddress(str, AbstractTransportAddress): class UnixTwistedTransport(DgramTwistedTransport): addressType = UnixTransportAddress + _lport = None # AbstractTwistedTransport API @@ -27,7 +28,7 @@ class UnixTwistedTransport(DgramTwistedTransport): raise error.CarrierError(sys.exc_info()[1]) return self - def openServerMode(self, iface=None): + def openServerMode(self, iface): try: self._lport = reactor.listenUNIXDatagram(iface, self) except Exception: @@ -36,8 +37,10 @@ class UnixTwistedTransport(DgramTwistedTransport): return self def closeTransport(self): - d = self._lport.stopListening() - d and d.addCallback(lambda x: None) + if self._lport is not None: + d = self._lport.stopListening() + if d: + d.addCallback(lambda x: None) DgramTwistedTransport.closeTransport(self) UnixTransport = UnixTwistedTransport diff --git a/pysnmp/carrier/twisted/dispatch.py b/pysnmp/carrier/twisted/dispatch.py index 4cadec32..04cf9fae 100644 --- a/pysnmp/carrier/twisted/dispatch.py +++ b/pysnmp/carrier/twisted/dispatch.py @@ -17,8 +17,10 @@ from twisted.internet import reactor, task from pysnmp.carrier.base import AbstractTransportDispatcher from pysnmp.error import PySnmpError + class TwistedDispatcher(AbstractTransportDispatcher): """TransportDispatcher based on twisted.internet.reactor""" + def __init__(self, *args, **kwargs): AbstractTransportDispatcher.__init__(self) self.__transportCount = 0 -- cgit v1.2.1