本文整理匯總了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()