diff --git a/pyagentx/agent.py b/pyagentx/agent.py index b6c0e2a..a989f7e 100644 --- a/pyagentx/agent.py +++ b/pyagentx/agent.py @@ -11,8 +11,7 @@ def emit(self, record): # -------------------------------------------- import time -import Queue -import inspect +import queue as Queue import pyagentx from pyagentx.updater import Updater @@ -31,7 +30,7 @@ def __init__(self): self._threads = [] def register(self, oid, class_, freq=10): - if Updater not in inspect.getmro(class_): + if Updater not in class_.__bases__: raise AgentError('Class given isn\'t an updater') # cleanup and test oid try: diff --git a/pyagentx/network.py b/pyagentx/network.py index 9711398..1a70311 100644 --- a/pyagentx/network.py +++ b/pyagentx/network.py @@ -13,7 +13,7 @@ def emit(self, record): import socket import time import threading -import Queue +import queue as Queue import pyagentx from pyagentx.pdu import PDU @@ -84,16 +84,16 @@ def _get_updates(self): update_oid = item['oid'] update_data = item['data'] # clear values with prefix oid - for oid in self.data.keys(): + for oid in list(self.data.keys()): if oid.startswith(update_oid): del(self.data[oid]) # insert updated value - for row in update_data.values(): + for row in list(update_data.values()): oid = "%s.%s" % (update_oid, row['name']) self.data[oid] = {'name': oid, 'type':row['type'], 'value':row['value']} # recalculate reverse index if data changed - self.data_idx = sorted(self.data.keys(), key=lambda k: tuple(int(part) for part in k.split('.'))) + self.data_idx = sorted(list(self.data.keys()), key=lambda k: tuple(int(part) for part in k.split('.'))) except Queue.Empty: break @@ -221,17 +221,17 @@ def _start_network(self): elif request.type == pyagentx.AGENTX_COMMITSET_PDU: - for handler in self._sethandlers.values(): + for handler in list(self._sethandlers.values()): handler.network_commit(request.session_id, request.transaction_id) logger.info("Received COMMITSET PDU") elif request.type == pyagentx.AGENTX_UNDOSET_PDU: - for handler in self._sethandlers.values(): + for handler in list(self._sethandlers.values()): handler.network_undo(request.session_id, request.transaction_id) logger.info("Received UNDOSET PDU") elif request.type == pyagentx.AGENTX_CLEANUPSET_PDU: - for handler in self._sethandlers.values(): + for handler in list(self._sethandlers.values()): handler.network_cleanup(request.session_id, request.transaction_id) logger.info("Received CLEANUP PDU") diff --git a/pyagentx/pdu.py b/pyagentx/pdu.py index 0af8e82..9d62c73 100644 --- a/pyagentx/pdu.py +++ b/pyagentx/pdu.py @@ -69,9 +69,10 @@ def encode_oid(self, oid, include=0): def encode_octet(self, octet): buf = struct.pack('!L', len(octet)) - buf += str(octet) + buf += octet padding = ( 4 - ( len(octet) % 4 ) ) % 4 - buf += chr(0)* padding + # buf += chr(0)* padding + buf += b'0' * padding return buf @@ -107,14 +108,14 @@ def encode_header(self, pdu_type, payload_length=0, flags=0): def encode(self): - buf = '' + buf = b'' if self.type == pyagentx.AGENTX_OPEN_PDU: # timeout buf += struct.pack('!BBBB', 5, 0, 0, 0) # agent OID buf += struct.pack('!L', 0) # Agent Desc - buf += self.encode_octet('MyAgent') + buf += self.encode_octet(b'MyAgent') elif self.type == pyagentx.AGENTX_PING_PDU: # No extra data @@ -169,7 +170,7 @@ def decode_oid(self): sub_ids.append(t[0]) oid = '.'.join(str(i) for i in sub_ids) return oid, ret['include'] - except Exception, e: + except Exception as e: logger.exception('Invalid packing OID header') logger.debug('%s' % pprint.pformat(self.decode_buf)) @@ -196,7 +197,7 @@ def decode_octet(self): buf = self.decode_buf[:l] self.decode_buf = self.decode_buf[l+padding:] return buf - except Exception, e: + except Exception as e: logger.exception('Invalid packing octet header') @@ -204,7 +205,7 @@ def decode_value(self): try: vtype,_ = struct.unpack('!HH', self.decode_buf[:4]) self.decode_buf = self.decode_buf[4:] - except Exception, e: + except Exception as e: logger.exception('Invalid packing value header') oid,_ = self.decode_oid() if vtype in [pyagentx.TYPE_INTEGER, pyagentx.TYPE_COUNTER32, pyagentx.TYPE_GAUGE32, pyagentx.TYPE_TIMETICKS]: @@ -252,7 +253,7 @@ def decode_header(self): context = self.decode_octet() logger.debug('Context: %s' % context) return ret - except Exception, e: + except Exception as e: logger.exception('Invalid packing: %d' % len(self.decode_buf)) logger.debug('%s' % pprint.pformat(self.decode_buf)) diff --git a/pyagentx/updater.py b/pyagentx/updater.py index 5fb06d4..803d765 100644 --- a/pyagentx/updater.py +++ b/pyagentx/updater.py @@ -12,7 +12,7 @@ def emit(self, record): import time import threading -import Queue +import queue as Queue import pyagentx