get subnets is now cross OS since it's identical + remove broadcast key since we don't have anything to do with it.

This commit is contained in:
Daniel Goldberg 2017-09-11 16:57:37 +03:00
parent 7b4fb5d4f0
commit 1bd633a0b1
1 changed files with 18 additions and 28 deletions

View File

@ -9,14 +9,8 @@ import netifaces
from subprocess import check_output from subprocess import check_output
from random import randint from random import randint
if sys.platform == "win32":
def local_ips(): def get_host_subnets():
local_hostname = socket.gethostname()
return socket.gethostbyname_ex(local_hostname)[2]
def get_host_subnets():
ipv4_nets = [netifaces.ifaddresses(interface)[netifaces.AF_INET] ipv4_nets = [netifaces.ifaddresses(interface)[netifaces.AF_INET]
for interface in netifaces.interfaces() for interface in netifaces.interfaces()
if netifaces.AF_INET in netifaces.ifaddresses(interface) if netifaces.AF_INET in netifaces.ifaddresses(interface)
@ -27,9 +21,19 @@ if sys.platform == "win32":
ipv4_nets = [network for network in ipv4_nets if network['addr'] != '127.0.0.1'] ipv4_nets = [network for network in ipv4_nets if network['addr'] != '127.0.0.1']
# remove auto conf # remove auto conf
ipv4_nets = [network for network in ipv4_nets if not network['addr'].startswith('169.254')] ipv4_nets = [network for network in ipv4_nets if not network['addr'].startswith('169.254')]
for network in ipv4_nets:
if 'broadcast' in network:
network.pop('broadcast')
return ipv4_nets return ipv4_nets
if sys.platform == "win32":
def local_ips():
local_hostname = socket.gethostname()
return socket.gethostbyname_ex(local_hostname)[2]
def get_routes(): def get_routes():
raise NotImplementedError() raise NotImplementedError()
@ -37,20 +41,6 @@ else:
from fcntl import ioctl from fcntl import ioctl
def get_host_subnets():
ipv4_nets = [netifaces.ifaddresses(interface)[netifaces.AF_INET]
for interface in netifaces.interfaces()
if netifaces.AF_INET in netifaces.ifaddresses(interface)
]
# flatten
ipv4_nets = list(itertools.chain.from_iterable(ipv4_nets))
# remove loopback
ipv4_nets = [network for network in ipv4_nets if network['addr'] != '127.0.0.1']
# remove auto conf
ipv4_nets = [network for network in ipv4_nets if not network['addr'].startswith('169.254')]
return ipv4_nets
def local_ips(): def local_ips():
ipv4_nets = get_host_subnets() ipv4_nets = get_host_subnets()
valid_ips = [network['addr'] for network in ipv4_nets] valid_ips = [network['addr'] for network in ipv4_nets]