def select_subnet_for_ip(ip, subnets): for subnet in subnets: if ipaddr.IPAddress(ip) in ipaddr.IPv4Network(subnet.address): return subnet