本文整理汇总了Python中socket.if_nametoindex方法的典型用法代码示例。如果您正苦于以下问题:Python socket.if_nametoindex方法的具体用法?Python socket.if_nametoindex怎么用?Python socket.if_nametoindex使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket
的用法示例。
在下文中一共展示了socket.if_nametoindex方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _create_ipv6_sockets
# 需要导入模块: import socket [as 别名]
# 或者: from socket import if_nametoindex [as 别名]
def _create_ipv6_sockets(loopback_enabled):
# Open a multicast send socket, with IP_MULTICAST_LOOP enabled or disabled as requested.
intf_name = find_ethernet_interface()
intf_index = socket.if_nametoindex(intf_name)
mcast_address = "ff02::abcd:99"
port = 30000
group = (mcast_address, port)
txsock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
txsock.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
txsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, intf_index)
if loopback_enabled:
txsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 1)
else:
txsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 0)
txsock.connect(group)
# Open a multicast receive socket and join the group
rxsock = socket.socket(socket.AF_INET6, socket.SOCK_DGRAM, socket.IPPROTO_UDP)
req = struct.pack("=16si", socket.inet_pton(socket.AF_INET6, mcast_address), intf_index)
if platform.system() == "Darwin":
rxsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, req)
else:
rxsock.setsockopt(socket.IPPROTO_IPV6, socket.IPV6_ADD_MEMBERSHIP, req)
rxsock.bind(("::", port))
return (txsock, rxsock)
示例2: init_v4
# 需要导入模块: import socket [as 别名]
# 或者: from socket import if_nametoindex [as 别名]
def init_v4(self):
idx = socket.if_nametoindex(self.interface.name)
self.multicast_address = (WSD_MCAST_GRP_V4, WSD_UDP_PORT)
# v4: member_request (ip_mreqn) = { multicast_addr, intf_addr, idx }
mreq = (
socket.inet_pton(self.family, WSD_MCAST_GRP_V4) +
socket.inet_pton(self.family, self.address) +
struct.pack('@I', idx))
self.recv_socket.setsockopt(
socket.IPPROTO_IP, socket.IP_ADD_MEMBERSHIP, mreq)
try:
self.recv_socket.bind((WSD_MCAST_GRP_V4, WSD_UDP_PORT))
except OSError:
self.recv_socket.bind(('', WSD_UDP_PORT))
self.send_socket.setsockopt(
socket.IPPROTO_IP, socket.IP_MULTICAST_IF, mreq)
self.send_socket.setsockopt(
socket.IPPROTO_IP, socket.IP_MULTICAST_LOOP, 0)
self.send_socket.setsockopt(
socket.IPPROTO_IP, socket.IP_MULTICAST_TTL, args.hoplimit)
self.listen_address = (self.address, WSD_HTTP_PORT)
示例3: __init__
# 需要导入模块: import socket [as 别名]
# 或者: from socket import if_nametoindex [as 别名]
def __init__(self, interface_name, local_port, ipv4, multicast_address, remote_address,
receive_function, log, log_id):
self._interface_name = interface_name
self._local_port = local_port
self._ipv4 = ipv4 # IPv4 if True, IPv6 if False
self._remote_address = remote_address
self._multicast_address = multicast_address # Unicast socket if None
self._receive_function = receive_function
self._log = log
self._log_id = log_id
self._local_ipv4_address = utils.interface_ipv4_address(interface_name)
self._local_ipv6_address = utils.interface_ipv6_address(interface_name)
try:
self._interface_index = socket.if_nametoindex(interface_name)
except (IOError, OSError) as err:
self.warning("Could determine index of interface %s: %s", interface_name, err)
self._interface_index = None
if ipv4:
if self._multicast_address:
self.sock = self.create_socket_ipv4_rx_mcast()
else:
self.sock = self.create_socket_ipv4_rx_ucast()
else:
if self._multicast_address:
self.sock = self.create_socket_ipv6_rx_mcast()
else:
self.sock = self.create_socket_ipv6_rx_ucast()
if self.sock:
scheduler.SCHEDULER.register_handler(self)
示例4: if_nametoindex
# 需要导入模块: import socket [as 别名]
# 或者: from socket import if_nametoindex [as 别名]
def if_nametoindex(ifname):
return _if_nametoindex(to_utf8(ifname))
### Sockets / Network Interfaces
示例5: addsockaddr
# 需要导入模块: import socket [as 别名]
# 或者: from socket import if_nametoindex [as 别名]
def addsockaddr(sock, address):
"""Configure physical-layer multicasting or promiscuous mode for `sock`.
If `addr` is None, promiscuous mode is configured. Otherwise `addr`
should be a tuple of up to 8 bytes to configure that multicast address.
"""
# pylint:disable=attribute-defined-outside-init
mreq = struct_packet_mreq()
mreq.mr_ifindex = if_nametoindex(getifname(sock))
if address is None:
mreq.mr_type = PACKET_MR_PROMISC
else:
mreq.mr_type = PACKET_MR_MULTICAST
mreq.mr_alen = len(address)
mreq.mr_address = address
sock.setsockopt(SOL_PACKET, PACKET_ADD_MEMBERSHIP, mreq)
示例6: test_interface
# 需要导入模块: import socket [as 别名]
# 或者: from socket import if_nametoindex [as 别名]
def test_interface(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
try:
route_cmd = os.popen("ip -o -4 route show to default")
default_route = route_cmd.read()
finally:
route_cmd.close()
my_interface = default_route.split()[4]
try:
socket.if_nametoindex(my_interface) # test if the interface exists.
except OSError:
self.fail('Interface Name Error: {}'.format(my_interface))
command_line_ping3.main(['-I', my_interface, 'example.com'])
self.assertRegex(fake_out.getvalue(), r".*[0-9]+ms.*")
示例7: test_ping_interface
# 需要导入模块: import socket [as 别名]
# 或者: from socket import if_nametoindex [as 别名]
def test_ping_interface(self):
try:
route_cmd = os.popen("ip -o -4 route show to default")
default_route = route_cmd.read()
finally:
route_cmd.close()
my_interface = default_route.split()[4]
try:
socket.if_nametoindex(my_interface) # test if the interface exists.
except OSError:
self.fail('Interface Name Error: {}'.format(my_interface))
dest_addr = "example.com"
delay = ping3.ping(dest_addr, interface=my_interface)
self.assertIsInstance(delay, float)
示例8: test_verbose_ping_interface
# 需要导入模块: import socket [as 别名]
# 或者: from socket import if_nametoindex [as 别名]
def test_verbose_ping_interface(self):
with patch("sys.stdout", new=io.StringIO()) as fake_out:
try:
route_cmd = os.popen("ip -o -4 route show to default")
default_route = route_cmd.read()
finally:
route_cmd.close()
my_interface = default_route.split()[4]
try:
socket.if_nametoindex(my_interface) # test if the interface exists.
except OSError:
self.fail('Interface Name Error: {}'.format(my_interface))
dest_addr = "example.com"
ping3.verbose_ping(dest_addr, interface=my_interface)
self.assertRegex(fake_out.getvalue(), r".*[0-9]+ms.*")
示例9: testInterfaceNameIndex
# 需要导入模块: import socket [as 别名]
# 或者: from socket import if_nametoindex [as 别名]
def testInterfaceNameIndex(self):
interfaces = socket.if_nameindex()
for index, name in interfaces:
self.assertIsInstance(index, int)
self.assertIsInstance(name, str)
# interface indices are non-zero integers
self.assertGreater(index, 0)
_index = socket.if_nametoindex(name)
self.assertIsInstance(_index, int)
self.assertEqual(index, _index)
_name = socket.if_indextoname(index)
self.assertIsInstance(_name, str)
self.assertEqual(name, _name)
示例10: testInvalidInterfaceNameIndex
# 需要导入模块: import socket [as 别名]
# 或者: from socket import if_nametoindex [as 别名]
def testInvalidInterfaceNameIndex(self):
# test nonexistent interface index/name
self.assertRaises(OSError, socket.if_indextoname, 0)
self.assertRaises(OSError, socket.if_nametoindex, '_DEADBEEF')
# test with invalid values
self.assertRaises(TypeError, socket.if_nametoindex, 0)
self.assertRaises(TypeError, socket.if_indextoname, '_DEADBEEF')
示例11: init_v6
# 需要导入模块: import socket [as 别名]
# 或者: from socket import if_nametoindex [as 别名]
def init_v6(self):
idx = socket.if_nametoindex(self.interface.name)
self.multicast_address = (WSD_MCAST_GRP_V6, WSD_UDP_PORT, 0x575C, idx)
# v6: member_request = { multicast_addr, intf_idx }
mreq = (
socket.inet_pton(self.family, WSD_MCAST_GRP_V6) +
struct.pack('@I', idx))
self.recv_socket.setsockopt(
socket.IPPROTO_IPV6, socket.IPV6_JOIN_GROUP, mreq)
self.recv_socket.setsockopt(
socket.IPPROTO_IPV6, socket.IPV6_V6ONLY, 1)
# bind to network interface, i.e. scope and handle OS differences,
# see Stevens: Unix Network Programming, Section 21.6, last paragraph
try:
self.recv_socket.bind((WSD_MCAST_GRP_V6, WSD_UDP_PORT, 0, idx))
except OSError:
self.recv_socket.bind(('::', 0, 0, idx))
self.send_socket.setsockopt(
socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_LOOP, 0)
self.send_socket.setsockopt(
socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_HOPS, args.hoplimit)
self.send_socket.setsockopt(
socket.IPPROTO_IPV6, socket.IPV6_MULTICAST_IF, idx)
self.transport_address = '[{0}]'.format(self.address)
self.listen_address = (self.address, WSD_HTTP_PORT, 0, idx)
示例12: get_adapters
# 需要导入模块: import socket [as 别名]
# 或者: from socket import if_nametoindex [as 别名]
def get_adapters(include_unconfigured=False):
addr0 = addr = ctypes.POINTER(ifaddrs)()
retval = libc.getifaddrs(ctypes.byref(addr))
if retval != 0:
eno = ctypes.get_errno()
raise OSError(eno, os.strerror(eno))
ips = collections.OrderedDict()
def add_ip(adapter_name, ip):
if not adapter_name in ips:
try:
index = socket.if_nametoindex(adapter_name)
except (OSError, AttributeError):
index = None
ips[adapter_name] = shared.Adapter(adapter_name, adapter_name, [],
index=index)
if ip is not None:
ips[adapter_name].ips.append(ip)
while addr:
name = addr[0].ifa_name
if sys.version_info[0] > 2:
name = name.decode(encoding='UTF-8')
ip = shared.sockaddr_to_ip(addr[0].ifa_addr)
if ip:
if addr[0].ifa_netmask and not addr[0].ifa_netmask[0].sa_familiy:
addr[0].ifa_netmask[0].sa_familiy = addr[0].ifa_addr[0].sa_familiy
netmask = shared.sockaddr_to_ip(addr[0].ifa_netmask)
if isinstance(netmask, tuple):
netmask = netmask[0]
if sys.version_info[0] > 2:
netmaskStr = str(netmask)
else:
netmaskStr = unicode(netmask)
prefixlen = shared.ipv6_prefixlength(ipaddress.IPv6Address(netmaskStr))
else:
if sys.version_info[0] > 2:
netmaskStr = str('0.0.0.0/' + netmask)
else:
netmaskStr = unicode('0.0.0.0/' + netmask)
prefixlen = ipaddress.IPv4Network(netmaskStr).prefixlen
ip = shared.IP(ip, prefixlen, name)
add_ip(name, ip)
else:
if include_unconfigured:
add_ip(name, None)
addr = addr[0].ifa_next
libc.freeifaddrs(addr0)
return ips.values()