import socket from infosec import Infosec import sys import binascii class Peer(object): def __init__(self, server, peer_entry): self.handles = peer_entry["handles"] self.keys = peer_entry["keys"] self.peer_id = peer_entry["peer_id"] self.server = server self.address = peer_entry["address"] self.port = peer_entry["port"] self.socket = self.server.udp_server_socket self.infosec = Infosec(server) def get_key(self): if len(self.keys) > 0: return self.keys[0] else: return None def send(self, msg): try: full_message = str.encode(msg["body"]) self.server.print_debug("sending message: %s" % full_message) signed_packet_bytes = self.infosec.pack(self, msg) self.socket.sendto(signed_packet_bytes, (self.address, self.port)) except Exception as ex: print("Exception while attempting to encode message: %s" % ex)