本文整理匯總了Python中netifaces.gateways方法的典型用法代碼示例。如果您正苦於以下問題:Python netifaces.gateways方法的具體用法?Python netifaces.gateways怎麽用?Python netifaces.gateways使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類netifaces
的用法示例。
在下文中一共展示了netifaces.gateways方法的15個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: configure
# 需要導入模塊: import netifaces [as 別名]
# 或者: from netifaces import gateways [as 別名]
def configure(protocol, port, pipes, interface):
remove_all()
reactor.addSystemEventTrigger('after', 'shutdown', remove_all)
# gets default (outward-facing) network interface (e.g. deciding which of
# eth0, eth1, wlan0 is being used by the system to connect to the internet)
if interface == "auto":
interface = netifaces.gateways()['default'][netifaces.AF_INET][1]
else:
if interface not in netifaces.interfaces():
raise ValueError("Given interface does not exist.", interface)
add(protocol, port, interface)
manager = libnetfilter_queue.Manager()
manager.bind(UP_QUEUE, packet_handler(manager, pipes.up))
manager.bind(DOWN_QUEUE, packet_handler(manager, pipes.down))
reader = abstract.FileDescriptor()
reader.doRead = manager.process
reader.fileno = lambda: manager.fileno
reactor.addReader(reader)
示例2: start_automatic
# 需要導入模塊: import netifaces [as 別名]
# 或者: from netifaces import gateways [as 別名]
def start_automatic(self, scan_filter: GatewayScanFilter):
"""Start GatewayScanner and connect to the found device."""
gatewayscanner = GatewayScanner(self.xknx, scan_filter=scan_filter)
gateways = await gatewayscanner.scan()
if not gateways:
raise XKNXException("No Gateways found")
gateway = gateways[0]
if gateway.supports_tunnelling and \
scan_filter.routing is not True:
await self.start_tunnelling(gateway.local_ip,
gateway.ip_addr,
gateway.port,
self.connection_config.auto_reconnect,
self.connection_config.auto_reconnect_wait)
elif gateway.supports_routing:
bind_to_multicast_addr = get_os_name() != "Darwin" # = Mac OS
await self.start_routing(gateway.local_ip, bind_to_multicast_addr)
示例3: get_iface
# 需要導入模塊: import netifaces [as 別名]
# 或者: from netifaces import gateways [as 別名]
def get_iface():
'''
Grabs an interface so we can grab the IP off that interface
'''
try:
iface = netifaces.gateways()['default'][netifaces.AF_INET][1]
except:
ifaces = []
for iface in netifaces.interfaces():
# list of ipv4 addrinfo dicts
ipv4s = netifaces.ifaddresses(iface).get(netifaces.AF_INET, [])
for entry in ipv4s:
addr = entry.get('addr')
if not addr:
continue
if not (iface.startswith('lo') or addr.startswith('127.')):
ifaces.append(iface)
# Just get the first interface
iface = ifaces[0]
return iface
示例4: get_iface
# 需要導入模塊: import netifaces [as 別名]
# 或者: from netifaces import gateways [as 別名]
def get_iface():
'''
Gets the right interface
'''
try:
iface = netifaces.gateways()['default'][netifaces.AF_INET][1]
except:
ifaces = []
for iface in netifaces.interfaces():
# list of ipv4 addrinfo dicts
ipv4s = netifaces.ifaddresses(iface).get(netifaces.AF_INET, [])
for entry in ipv4s:
addr = entry.get('addr')
if not addr:
continue
if not (iface.startswith('lo') or addr.startswith('127.')):
ifaces.append(iface)
iface = ifaces[0]
return iface
示例5: get_iface
# 需要導入模塊: import netifaces [as 別名]
# 或者: from netifaces import gateways [as 別名]
def get_iface(self):
'''
Gets the interface with an IP address connected to a default gateway
'''
try:
iface = netifaces.gateways()['default'][netifaces.AF_INET][1]
except:
ifaces = []
for iface in netifaces.interfaces():
# list of ipv4 addrinfo dicts
ipv4s = netifaces.ifaddresses(iface).get(netifaces.AF_INET, [])
for entry in ipv4s:
addr = entry.get('addr')
if not addr:
continue
if not (iface.startswith('lo') or addr.startswith('127.')):
ifaces.append(iface)
iface = ifaces[0]
return iface
示例6: run
# 需要導入模塊: import netifaces [as 別名]
# 或者: from netifaces import gateways [as 別名]
def run(self):
while 1:
gws = netifaces.gateways()
cgw = gws['default'][netifaces.AF_INET][0]
if not homenet.gateway:
homenet.gateway = cgw
else:
if homenet.gateway != cgw:
utils.reconfigure_network(homenet.gateway, cgw)
homenet.gateway = cgw
try:
with lock:
utils.save_pkl_object(homenet, "homenet.pkl")
except Exception as e:
log.debug('FG-ERROR ' + str(e.__doc__) + " - " + str(e))
utils.reboot_appliance()
else:
pass
time.sleep(10)
示例7: best_remote_address
# 需要導入模塊: import netifaces [as 別名]
# 或者: from netifaces import gateways [as 別名]
def best_remote_address():
"""
Returns the "best" non-localback IP address for the local host, if
possible. The "best" IP address is that bound to either the default
gateway interface, if any, else the arbitrary first interface found.
IPv4 is preferred, but IPv6 will be used if no IPv4 interfaces/addresses
are available.
"""
default_gateway = net.gateways().get("default", {})
default_interface = default_gateway.get(net.AF_INET, (None, None))[1] \
or default_gateway.get(net.AF_INET6, (None, None))[1] \
or net.interfaces()[0]
interface_addresses = net.ifaddresses(default_interface).get(net.AF_INET) \
or net.ifaddresses(default_interface).get(net.AF_INET6) \
or []
addresses = [
address["addr"]
for address in interface_addresses
if address.get("addr")
]
return addresses[0] if addresses else None
示例8: get_iface
# 需要導入模塊: import netifaces [as 別名]
# 或者: from netifaces import gateways [as 別名]
def get_iface():
'''
Gets the right interface for Responder
'''
try:
iface = netifaces.gateways()['default'][netifaces.AF_INET][1]
except:
ifaces = []
for iface in netifaces.interfaces():
# list of ipv4 addrinfo dicts
ipv4s = netifaces.ifaddresses(iface).get(netifaces.AF_INET, [])
for entry in ipv4s:
addr = entry.get('addr')
if not addr:
continue
if not (iface.startswith('lo') or addr.startswith('127.')):
ifaces.append(iface)
iface = ifaces[0]
return iface
示例9: get_default_gateway_ip
# 需要導入模塊: import netifaces [as 別名]
# 或者: from netifaces import gateways [as 別名]
def get_default_gateway_ip():
"""Return the default gateway IP."""
gateways = netifaces.gateways()
defaults = gateways.get("default")
if not defaults:
return
def default_ip(family):
gw_info = defaults.get(family)
if not gw_info:
return
addresses = netifaces.ifaddresses(gw_info[1]).get(family)
if addresses:
return addresses[0]["addr"]
return default_ip(netifaces.AF_INET) or default_ip(netifaces.AF_INET6)
示例10: get_machine_default_gateway_ip
# 需要導入模塊: import netifaces [as 別名]
# 或者: from netifaces import gateways [as 別名]
def get_machine_default_gateway_ip():
"""Return the default gateway IP for the machine."""
gateways = netifaces.gateways()
defaults = gateways.get("default")
if not defaults:
return
def default_ip(family):
gw_info = defaults.get(family)
if not gw_info:
return
addresses = netifaces.ifaddresses(gw_info[1]).get(family)
if addresses:
return addresses[0]["addr"]
return default_ip(netifaces.AF_INET) or default_ip(netifaces.AF_INET6)
示例11: get_default_gateway
# 需要導入模塊: import netifaces [as 別名]
# 或者: from netifaces import gateways [as 別名]
def get_default_gateway(interface="default"):
if sys.version_info < (3, 0, 0):
if type(interface) == str:
interface = unicode(interface)
else:
if type(interface) == bytes:
interface = interface.decode("utf-8")
if platform.system() == "Windows":
if interface == "default":
default_routes = [r for r in get_ipv4_routing_table()
if r[0] == '0.0.0.0']
if default_routes:
return default_routes[0][2]
try:
gws = netifaces.gateways()
if sys.version_info < (3, 0, 0):
return gws[interface][netifaces.AF_INET][0].decode("utf-8")
else:
return gws[interface][netifaces.AF_INET][0]
except:
# This can also mean the machine is directly accessible.
return None
示例12: _get_my_ipv4_address
# 需要導入模塊: import netifaces [as 別名]
# 或者: from netifaces import gateways [as 別名]
def _get_my_ipv4_address():
"""Figure out the best ipv4
"""
LOCALHOST = '127.0.0.1'
gtw = netifaces.gateways()
try:
interface = gtw['default'][netifaces.AF_INET][1]
except (KeyError, IndexError):
LOG.info('Could not determine default network interface, '
'using 127.0.0.1 for IPv4 address')
return LOCALHOST
try:
return netifaces.ifaddresses(interface)[netifaces.AF_INET][0]['addr']
except (KeyError, IndexError):
LOG.info('Could not determine IPv4 address for interface %s, '
'using 127.0.0.1',
interface)
except Exception as e:
LOG.info('Could not determine IPv4 address for '
'interface %(interface)s: %(error)s',
{'interface': interface, 'error': e})
return LOCALHOST
示例13: default_ipv4
# 需要導入模塊: import netifaces [as 別名]
# 或者: from netifaces import gateways [as 別名]
def default_ipv4():
try:
default_if = netifaces.gateways()['default'][netifaces.AF_INET][1]
return netifaces.ifaddresses(default_if)[netifaces.AF_INET][0]['addr']
except:
traceback.print_exc()
return None
示例14: get_gateways
# 需要導入模塊: import netifaces [as 別名]
# 或者: from netifaces import gateways [as 別名]
def get_gateways():
gateway_dict = {}
gws = netifaces.gateways()
for gw in gws:
try:
gateway_iface = gws[gw][netifaces.AF_INET]
gateway_ip, iface = gateway_iface[0], gateway_iface[1]
gw_list =[gateway_ip, iface]
gateway_dict[gw]=gw_list
except:
pass
return gateway_dict
示例15: get_networks
# 需要導入模塊: import netifaces [as 別名]
# 或者: from netifaces import gateways [as 別名]
def get_networks(gateways_dict):
networks_dict = {}
for key, value in gateways.iteritems():
gateway_ip, iface = value[0], value[1]
hwaddress, addr, broadcast, netmask = get_addresses(iface)
network = {'gateway': gateway_ip, 'hwaddr' : hwaddress, 'addr' : addr, ' broadcast' : broadcast, 'netmask' : netmask}
networks_dict[iface] = network
return networks_dict