def __init__(self, bind_address="INADDR_ANY", bind_port=5354, multicast_address="224.1.2.3", multicast_port=5355): if bind_address == "localhost": self.bind_address = net.get_local_ip_address() else: self.bind_address = bind_address self.bind_port = bind_port self.multicast_address = multicast_address self.multicast_port = multicast_port self.multicast_group = (multicast_address, multicast_port) self.sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM, socket.IPPROTO_UDP) try: self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, True) except AttributeError: pass if hasattr(self.sock, 'SO_REUSEPORT'): self.sock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEPORT, True) ttl = struct.pack('b', Multicast.ttl) self.sock.setsockopt(socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, ttl)
def send(self, message): localhost = network.get_local_ip_address() self.logger.debug('UDP send for host ' + localhost + ", message: " + message) self.logger.debug("To: " + self.dest_address + " / " + str(self.dest_port)) self.sock.sendto(message, (self.dest_address, self.dest_port))
def init_wrapper(transport_impl, bind_address, bind_port, receive_address, receive_port): exc_info = None logger = logging.getLogger('Transport: Initialize Client') if bind_address == "localhost": bind_address = net.get_local_ip_address() else: bind_address = bind_address if receive_address == "localhost": receive_address = net.get_local_ip_address() else: receive_address = receive_address logger.debug("Binding to address " + bind_address + " and port " + str(bind_port)) try: init(transport_impl, bind_address, bind_port, receive_address, receive_port) except Exception as err: import sys exc_info = sys.exc_info() print "ERROR OF TYPE: " + str(type(err)) # the exception instance print "Details: " + str(err) send_address = ", RECEIVE/DESTINATION ADDRESS AND PORT IS NOT DEFINED FOR THIS 'OUTPUT TYPE' SOCKET" if receive_address is not None: send_address = ", Receiver Address=" + receive_address + ", Receiver Port=" + str(receive_port) print "Socket Details: Bind Address=" + bind_address + ", Bind Port=" + str(bind_port) + send_address finally: # Display the *original* exception if exc_info is not None: import traceback traceback.print_exception(*exc_info) del exc_info exit(1) transport_impl.bind_address = bind_address transport_impl.bind_port = bind_port
def __init__(self, target_method, bind_address="INADDR_ANY", bind_port=5354): self.is_running = False self.target_method = target_method self.logger = logging.getLogger('UDPServer') localhost = network.get_local_ip_address() self.logger.debug('UDP __init__ for host ' + localhost + ' using bind address: ' + bind_address + "/" + str(bind_port)) SocketServer.UDPServer.allow_reuse_address = True SocketServer.UDPServer.__init__(self, (str(bind_address), int(bind_port)), RequestHandler) return
def init_wrapper(transport_impl, target_method, bind_address, bind_port, multicast_address=None, multicast_port=None): logger = logging.getLogger('Transport: Initialize Server') exc_info = None if bind_address == "localhost": bind_address = net.get_local_ip_address() else: bind_address = bind_address logger.debug("Init Server: Binding to address " + bind_address + " and port " + str(bind_port)) try: if multicast_address is None: init(transport_impl, target_method, bind_address, bind_port) else: init(transport_impl, target_method, bind_address, bind_port, multicast_address, multicast_port) except socket.error as socket_error: import sys exc_info = sys.exc_info() print "ERROR OF TYPE: " + str(type(socket_error)) # the exception instance print "Details: " + str(socket_error) mcast_text = "" if multicast_address is not None: mcast_text = ", Multicast Address=" + multicast_address + ", Multicast Port=" + str(multicast_port) print "Socket Details: Bind Address=" + bind_address + ", Bind Port=" + str(bind_port) + mcast_text finally: # Display the *original* exception if exc_info is not None: import traceback traceback.print_exception(*exc_info) del exc_info exit(1) transport_impl.bind_address = bind_address transport_impl.bind_port = bind_port