2026-01-09 14:40:20 +08:00
|
|
|
|
import socket
|
|
|
|
|
|
import psutil
|
|
|
|
|
|
import re
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class NetworkIPGetter:
|
|
|
|
|
|
"""网络IP获取器"""
|
|
|
|
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
|
|
self.interfaces = psutil.net_if_addrs()
|
|
|
|
|
|
|
|
|
|
|
|
def get_wlan_ip(self):
|
2026-02-03 13:09:01 +08:00
|
|
|
|
"""获取无线网卡IP(优先)。Windows: WLAN/Wi-Fi;macOS: en0/en1(通常为 Wi-Fi)"""
|
|
|
|
|
|
return self._get_interface_ip(['WLAN', 'Wi-Fi', 'WiFi', 'Wireless', 'en0', 'en1'])
|
2026-01-09 14:40:20 +08:00
|
|
|
|
|
|
|
|
|
|
def get_ethernet_ip(self):
|
2026-02-03 13:09:01 +08:00
|
|
|
|
"""获取有线网卡IP。Windows: Ethernet;Linux: eth/enp;macOS: en2/en3 等"""
|
|
|
|
|
|
return self._get_interface_ip(['Ethernet', '以太网', 'eth', 'enp', 'en2', 'en3', 'en4'])
|
2026-01-09 14:40:20 +08:00
|
|
|
|
|
|
|
|
|
|
def _get_interface_ip(self, keywords):
|
2026-02-03 13:09:01 +08:00
|
|
|
|
"""根据关键词查找接口IP(按 keywords 顺序优先匹配)"""
|
|
|
|
|
|
for keyword in keywords:
|
|
|
|
|
|
kw_lower = keyword.lower()
|
|
|
|
|
|
for iface_name, addrs in self.interfaces.items():
|
|
|
|
|
|
if kw_lower in iface_name.lower():
|
|
|
|
|
|
for addr in addrs:
|
|
|
|
|
|
if addr.family == socket.AF_INET:
|
|
|
|
|
|
if not self._is_virtual_ip(addr.address):
|
|
|
|
|
|
return addr.address
|
2026-01-09 14:40:20 +08:00
|
|
|
|
return None
|
|
|
|
|
|
|
|
|
|
|
|
def _is_virtual_ip(self, ip):
|
|
|
|
|
|
"""判断是否为虚拟/私有IP"""
|
|
|
|
|
|
private_patterns = [
|
|
|
|
|
|
r'^127\.', # 回环地址
|
|
|
|
|
|
r'^192\.168\.', # 私有C类
|
|
|
|
|
|
r'^172\.(1[6-9]|2[0-9]|3[0-1])\.', # 私有B类
|
|
|
|
|
|
r'^10\.', # 私有A类
|
|
|
|
|
|
r'^169\.254\.', # 链路本地
|
|
|
|
|
|
]
|
|
|
|
|
|
|
|
|
|
|
|
for pattern in private_patterns:
|
|
|
|
|
|
if re.match(pattern, ip):
|
|
|
|
|
|
return False # 私有IP也是有效的WLAN地址
|
|
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
|
|
|
def get_preferred_ip(self):
|
|
|
|
|
|
"""获取首选网络IP(优先WLAN,其次有线)"""
|
|
|
|
|
|
ip = self.get_wlan_ip()
|
|
|
|
|
|
if not ip:
|
|
|
|
|
|
ip = self.get_ethernet_ip()
|
|
|
|
|
|
return ip or "127.0.0.1"
|
|
|
|
|
|
|
|
|
|
|
|
def get_all_ips(self):
|
|
|
|
|
|
"""获取所有网络接口的IPv4地址"""
|
|
|
|
|
|
result = {}
|
|
|
|
|
|
for iface_name, addrs in self.interfaces.items():
|
|
|
|
|
|
for addr in addrs:
|
|
|
|
|
|
if addr.family == socket.AF_INET:
|
|
|
|
|
|
if iface_name not in result:
|
|
|
|
|
|
result[iface_name] = []
|
|
|
|
|
|
result[iface_name].append({
|
|
|
|
|
|
'address': addr.address,
|
|
|
|
|
|
'netmask': addr.netmask,
|
|
|
|
|
|
'broadcast': addr.broadcast
|
|
|
|
|
|
})
|
|
|
|
|
|
return result
|
|
|
|
|
|
|
|
|
|
|
|
|
2026-02-03 13:09:01 +08:00
|
|
|
|
# 使用示例
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
|
|
getter = NetworkIPGetter()
|
|
|
|
|
|
|
|
|
|
|
|
# 获取WLAN IP
|
|
|
|
|
|
wlan_ip = getter.get_wlan_ip()
|
|
|
|
|
|
print(f"WLAN IPv4地址: {wlan_ip}")
|
|
|
|
|
|
|
|
|
|
|
|
# 获取首选IP
|
|
|
|
|
|
main_ip = getter.get_preferred_ip()
|
|
|
|
|
|
print(f"首选IP地址: {main_ip}")
|
2026-01-09 14:40:20 +08:00
|
|
|
|
|