MCPcopy Create free account
hub / github.com/Tencent/CodeAnalysis / HostNetMgr

Class HostNetMgr

client/node/servertask/nodemgr.py:61–97  ·  view source on GitHub ↗

机器网卡信息管理

Source from the content-addressed store, hash-verified

59
60
61class HostNetMgr(object):
62 """机器网卡信息管理"""
63 def get_net_if_addr(self):
64 """获取多网卡 mac 和 ip 信息"""
65 result = []
66 net_addrs = psutil.net_if_addrs()
67 for adapter, snic_list in net_addrs.items():
68 mac = None # '无 mac 地址'
69 ipv4 = None # '无 ipv4 地址'
70 ipv6 = None # '无 ipv6 地址'
71 for snic in snic_list:
72 if snic.family.name in ['AF_LINK', 'AF_PACKET']:
73 mac = snic.address
74 if snic.family.name == "AF_INET":
75 ipv4 = snic.address
76 if snic.family.name == "AF_INET6":
77 ipv6 = snic.address
78 result.append({
79 'adapter': adapter,
80 "mac": mac,
81 'ipv4': ipv4,
82 'ipv6': ipv6
83 })
84 return result
85
86 def get_host_ip(self):
87 """获取本机ip"""
88 try:
89 net_info_list = self.get_net_if_addr()
90 for net_info in net_info_list:
91 mac = net_info["mac"]
92 ipv4 = net_info["ipv4"]
93 if mac and ipv4 and ipv4 != "127.0.0.1": # 有mac地址和ipv4地址,才算获取到当前在用的ip
94 return ipv4
95 except Exception as err:
96 LogPrinter.error("get host ip error: %s" % str(err))
97 return ""
98
99
100class HeartBeat(object):

Callers 1

_thread_beatMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected