本文整理汇总了Python中netifaces.interfaces方法的典型用法代码示例。如果您正苦于以下问题:Python netifaces.interfaces方法的具体用法?Python netifaces.interfaces怎么用?Python netifaces.interfaces使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类netifaces
的用法示例。
在下文中一共展示了netifaces.interfaces方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: video_invite
# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import interfaces [as 别名]
def video_invite():
global IsOpen, Version, AudioOpen
if Version == 4:
host_name = socket.gethostbyname(socket.getfqdn(socket.gethostname()))
else:
host_name = [i['addr'] for i in ifaddresses(interfaces()[-2]).setdefault(AF_INET6, [{'addr': 'No IP addr'}])][
-1]
invite = 'INVITE' + host_name + ':;' + user + ':;' + chat
s.send(invite.encode())
if not IsOpen:
vserver = vachat.Video_Server(10087, Version)
if AudioOpen:
aserver = vachat.Audio_Server(10088, Version)
aserver.start()
vserver.start()
IsOpen = True
示例2: get_interfaces
# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import interfaces [as 别名]
def get_interfaces():
interfaces = netifaces.interfaces()
interfaces.remove('lo')
out_interfaces = dict()
for interface in interfaces:
addrs = netifaces.ifaddresses(interface)
out_addrs = dict()
if netifaces.AF_INET in addrs.keys():
out_addrs["ipv4"] = addrs[netifaces.AF_INET]
if netifaces.AF_INET6 in addrs.keys():
out_addrs["ipv6"] = addrs[netifaces.AF_INET6]
out_interfaces[interface] = out_addrs
return out_interfaces
示例3: default_physical_interface
# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import interfaces [as 别名]
def default_physical_interface(self):
# When simulated interfaces are disabled, the interface names on nodes correspond to real
# interfaces on the host platform.
# When simulated interface are enabled, the interface names on nodes are "fake" i.e. they do
# not correspond to real interfaces on the host platform. All these simulated interfaces
# actually run on a single real interface, referred to as the physical interface. Traffic to
# and from different simulated interfaces are distinguished by using different multicast
# addresses and port numbers for each simulated interface.
# Pick the first interface with a broadcast IPv4 address (if any) as the default.
for intf_name in netifaces.interfaces():
addresses = netifaces.ifaddresses(intf_name)
if netifaces.AF_INET in addresses:
ipv4_addresses = addresses[netifaces.AF_INET]
for ipv4_address in ipv4_addresses:
if 'broadcast' in ipv4_address:
return intf_name
print("Cannot pick default physical interface: no broadcast interface found")
sys.exit(1)
示例4: find_internal_ip_on_device_network
# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import interfaces [as 别名]
def find_internal_ip_on_device_network(upnp_dev: upnpclient.upnp.Device) -> str:
"""
For a given UPnP device, return the internal IP address of this host machine that can
be used for a NAT mapping.
"""
parsed_url = urlparse(upnp_dev.location)
# Get an ipaddress.IPv4Network instance for the upnp device's network.
upnp_dev_net = ipaddress.ip_network(parsed_url.hostname + "/24", strict=False)
for iface in netifaces.interfaces():
for family, addresses in netifaces.ifaddresses(iface).items():
# TODO: Support IPv6 addresses as well.
if family != netifaces.AF_INET:
continue
for item in addresses:
if ipaddress.ip_address(item["addr"]) in upnp_dev_net:
return item["addr"]
raise NoInternalAddressMatchesDevice(device_hostname=parsed_url.hostname)
示例5: crawl
# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import interfaces [as 别名]
def crawl(self, container_id, avoid_setns=False, **kwargs):
"""
Start flow probe + data collector pairs on the interfaces of
the given container; collect the files that the collector
wrote and return their content.
"""
if time.time() > FprobeContainerCrawler.next_cleanup:
# we won't run the cleanup of old files the first time
# but let the crawler do one full round of picking up
# relevant files and then only we do a proper cleaning
if FprobeContainerCrawler.next_cleanup > 0:
FprobeContainerCrawler.remove_old_files(**kwargs)
self.cleanup(**kwargs)
FprobeContainerCrawler.next_cleanup = time.time() + 30
ifnames = self.start_container_fprobes(container_id, avoid_setns,
**kwargs)
return self.collect_files(container_id, ifnames, **kwargs)
示例6: cleanup
# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import interfaces [as 别名]
def cleanup(self, **kwargs):
"""
Check the available interfaces on the host versus those ones we
have flow probes running and remove those where the interface has
disappeared. We clean up the files with netlink data that were
written for those interfaces.
"""
devices = netifaces.interfaces()
lst = []
for ifname in CTProbeContainerCrawler.ifaces_monitored:
if ifname not in devices:
self.remove_datafiles(ifname, **kwargs)
else:
lst.append(ifname)
CTProbeContainerCrawler.ifaces_monitored = lst
示例7: connectivity_request
# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import interfaces [as 别名]
def connectivity_request(self):
"""Handles connectivity requests"""
# please note: normally we get an rcat argument, default: rcat=0
with Promise() as p:
blacklist = ['lo']
interfaces = netifaces.interfaces()
interface = next((x for x in interfaces if (x not in blacklist)),
None)
if interface is None:
p.reject(InterfaceNotFoundException(
"No interfaces found matching request"))
else:
p.fulfill((self._get_interface(interface), 0))
return p
示例8: _get_addresses
# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import interfaces [as 别名]
def _get_addresses(self, iface=None):
"""Get addresses of a given interface
:param iface: name of interface
:return: list of addresses
"""
if iface is None:
interfaces = netifaces.interfaces()
else:
interfaces = [iface]
addresses = []
for interface in interfaces:
n_addresses = netifaces.ifaddresses(interface)
addresses += self._get_addresses_from_ifaddresses(n_addresses)
# check if array has duplicates
# addresses = list(set(addresses))
return addresses
示例9: _get_interface
# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import interfaces [as 别名]
def _get_interface(self, name):
"""Returns an Interface object identified by name
:param name: name of interface
:return Interface: interface
:raise UnknownInterface: if interface was not found
"""
if name not in netifaces.interfaces():
raise InterfaceNotFoundException("%s was not found" % name)
else:
ifaddresses = netifaces.ifaddresses(name)
addresses = self._get_addresses_from_ifaddresses(ifaddresses)
try:
hwaddress = ifaddresses[netifaces.AF_LINK][0]["addr"]
except (IndexError, KeyError):
self.logger.debug("No hardware address found for %s!", name)
hwaddress = None
return Interface(name=name,
addresses=addresses,
hwaddress=hwaddress)
示例10: _get_addresses
# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import interfaces [as 别名]
def _get_addresses(self, family):
try:
return self.__cached_addresses[family]
except KeyError:
from netifaces import interfaces, ifaddresses
addresses = self.__cached_addresses[family] = set()
for interface in interfaces():
try:
ifdata = ifaddresses(interface)[family]
ifaddrs = [x.split("%")[0] for x in pluck("addr", ifdata)]
addresses.update(ifaddrs)
except KeyError:
pass
return addresses
示例11: get_broadcast_list
# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import interfaces [as 别名]
def get_broadcast_list():
"""Return all broadcast addresses.
E.g. WOL messages need to be sent from all NICs.
"""
brlist = ['<broadcast>']
ifaces = [iface for iface in netifaces.interfaces() if iface != 'lo']
for ifname in ifaces:
# An interface can have more than one address, even within the
# same family (AF_INET), or none, so check this case too.
addresses = netifaces.ifaddresses(ifname)
if netifaces.AF_INET not in addresses:
continue
for addr in addresses[netifaces.AF_INET]:
if 'broadcast' in addr:
brlist.append(addr['broadcast'])
return brlist
示例12: get_all_ips
# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import interfaces [as 别名]
def get_all_ips():
"""
Find all IPs for this machine.
:return: ``set`` of IP addresses (``IPAddress``).
"""
ips = set()
interfaces = netifaces.interfaces()
for interface in interfaces:
addresses = netifaces.ifaddresses(interface)
for address_family in (netifaces.AF_INET, netifaces.AF_INET6):
family_addresses = addresses.get(address_family)
if not family_addresses:
continue
for address in family_addresses:
ips.add(ipaddress_from_string(address['addr']))
return ips
示例13: get_host_devices
# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import interfaces [as 别名]
def get_host_devices(iface_prefix=''):
rst = {}
for ifacename in netifaces.interfaces():
if not ifacename.startswith(iface_prefix):
continue
addrs = netifaces.ifaddresses(ifacename)
if netifaces.AF_INET in addrs and netifaces.AF_LINK in addrs:
ips = [addr['addr'] for addr in addrs[netifaces.AF_INET]]
for ip in ips:
if is_ip4_loopback(ip):
break
else:
rst[ifacename] = {'INET': addrs[netifaces.AF_INET],
'LINK': addrs[netifaces.AF_LINK]}
return rst
示例14: find
# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import interfaces [as 别名]
def find(cls, zc=None, timeout=5, interfaces=InterfaceChoice.All):
"""
Return all of the advertised services on any local networks.
:param zc: Zeroconf() instance. Pass in if already have an
instance running or if non-default interfaces are needed
:param timeout: seconds to wait for any responses
:return: tuple of service type strings
"""
local_zc = zc or Zeroconf(interfaces=interfaces)
listener = cls()
browser = ServiceBrowser(
local_zc, '_services._dns-sd._udp.local.', listener=listener)
# wait for responses
time.sleep(timeout)
# close down anything we opened
if zc is None:
local_zc.close()
else:
browser.cancel()
return tuple(sorted(listener.found_services))
示例15: __init__
# 需要导入模块: import netifaces [as 别名]
# 或者: from netifaces import interfaces [as 别名]
def __init__(self,
data # Data suitable for this class
):
valid, message = data_is_valid(data)
if not valid:
raise ValueError("Invalid data: %s" % message)
self.cidrs = []
for iface in netifaces.interfaces():
ifaddrs = netifaces.ifaddresses(iface)
if netifaces.AF_INET in ifaddrs:
for ifaddr in ifaddrs[netifaces.AF_INET]:
if 'addr' in ifaddr:
self.cidrs.append(ipaddress.ip_network(unicode(ifaddr['addr'])))
if netifaces.AF_INET6 in ifaddrs:
for ifaddr in ifaddrs[netifaces.AF_INET6]:
if 'addr' in ifaddr:
#add v6 but remove stuff like %eth0 that gets thrown on end of some addrs
self.cidrs.append(ipaddress.ip_network(unicode(ifaddr['addr'].split('%')[0])))