本文整理汇总了Python中socket.if_nameindex方法的典型用法代码示例。如果您正苦于以下问题:Python socket.if_nameindex方法的具体用法?Python socket.if_nameindex怎么用?Python socket.if_nameindex使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类socket
的用法示例。
在下文中一共展示了socket.if_nameindex方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: local_ip4_addr_list
# 需要导入模块: import socket [as 别名]
# 或者: from socket import if_nameindex [as 别名]
def local_ip4_addr_list():
"""Return a set of IPv4 address
"""
assert os.name != 'nt', 'Do not support Windows rpc yet.'
nic = set()
for if_nidx in socket.if_nameindex():
name = if_nidx[1]
sock = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
try:
ip_of_ni = fcntl.ioctl(sock.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', name[:15].encode("UTF-8")))
except OSError as e:
if e.errno == 99: # EADDRNOTAVAIL
print("Warning!",
"Interface: {}".format(name),
"IP address not available for interface.",
sep='\n')
continue
else:
raise e
ip_addr = socket.inet_ntoa(ip_of_ni[20:24])
nic.add(ip_addr)
return nic
示例2: _local_ip4_addr_list
# 需要导入模块: import socket [as 别名]
# 或者: from socket import if_nameindex [as 别名]
def _local_ip4_addr_list(self):
"""Return a set of IPv4 address
"""
nic = set()
for ix in socket.if_nameindex():
name = ix[1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ip = socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', name[:15].encode("UTF-8")))[20:24])
nic.add(ip)
return nic
示例3: local_ip4_addr_list
# 需要导入模块: import socket [as 别名]
# 或者: from socket import if_nameindex [as 别名]
def local_ip4_addr_list():
"""Return a set of IPv4 address
"""
nic = set()
for ix in socket.if_nameindex():
name = ix[1]
s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
ip = socket.inet_ntoa(fcntl.ioctl(
s.fileno(),
0x8915, # SIOCGIFADDR
struct.pack('256s', name[:15].encode("UTF-8")))[20:24])
nic.add(ip)
return nic
示例4: testInterfaceNameIndex
# 需要导入模块: import socket [as 别名]
# 或者: from socket import if_nameindex [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)
示例5: get_if_list
# 需要导入模块: import socket [as 别名]
# 或者: from socket import if_nameindex [as 别名]
def get_if_list():
#return [ i[1] for i in socket.if_nameindex() ]
return netifaces.interfaces()