def get_server_address(port): for ip_obj in netutils.iter_ipv4_addresses(): net = IPv4Network(ip_obj["addr"] + "/" + ip_obj["netmask"], strict=False) if not net.is_loopback and check_ip(ip_obj["addr"], port): return ip_obj["addr"] return "localhost"
def get_message_server_address(request_addr): for ip_obj in netutils.iter_ipv4_addresses(): ours = IPv4Network(ip_obj["addr"] + "/" + ip_obj["netmask"], strict=False) theirs = IPv4Network(request_addr + "/" + ip_obj["netmask"], strict=False) if ours == theirs: return {"host": ip_obj["addr"], "port": 11023} return None
def test_valid_discovery_server(self): service = DiscoveryService(None) event = Event() service.notify_start = event.set Thread(target=service.on_service_start).start() event.wait() ip_addresses = [x["addr"] for x in netutils.iter_ipv4_addresses()] try: assert discover_message_server()[0] in ip_addresses finally: service.on_service_stop()
def test_get_message_server_address(self): ip_addr, port = "<broadcast>", 23034 client = socket.socket(socket.AF_INET, socket.SOCK_DGRAM) client.bind(('', 0)) client.setsockopt(socket.SOL_SOCKET, socket.SO_BROADCAST, 1) client.sendto("QUERY".encode('UTF-8'), (ip_addr, port)) client.settimeout(5) data, _ = client.recvfrom(1024) obj = json.loads(data.decode())["host"] assert obj in [x["addr"] for x in netutils.iter_ipv4_addresses()]
def discover(service, username, pwd): logger.info("Scanning for Dahua Cameras.") res = [] for ip_obj in netutils.iter_ipv4_addresses(): net = IPv4Network(ip_obj["addr"] + "/" + ip_obj["netmask"], strict=False) if net.is_loopback: continue for ip in net.hosts(): if ip <= IPv4Address("192.168.1.115"): continue if ip > IPv4Address("192.168.1.120"): continue if DahuaMessagingBridge.check_ip(ip): res.append(str(ip)) logger.info("Found %d Dahua Cameras.", len(res)) return [ DahuaMessagingBridge(service, str(x), username, pwd, y) for x in res for y in range(1, 5) ]