From b04ce0f16a4a46875bda12733d335dc6bb6aed7e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=90=A6?= Date: Tue, 26 Sep 2023 16:37:33 +0800 Subject: [PATCH] fix: The get_ip_address interface attempts to ignore the virtual network card address (cherry picked from commit 3708185727cb91ed9372f0e4f251f57bb81cb22e) --- airtest/core/win/win.py | 27 ++++++++++++++++++--------- 1 file changed, 18 insertions(+), 9 deletions(-) diff --git a/airtest/core/win/win.py b/airtest/core/win/win.py index 6d5d660a..a8377528 100644 --- a/airtest/core/win/win.py +++ b/airtest/core/win/win.py @@ -5,6 +5,7 @@ import subprocess import numpy import mss +import psutil from functools import wraps import win32api import pywintypes # noqa @@ -508,15 +509,23 @@ def get_ip_address(self): Returns: :py:obj:`str`: ip address """ - hostname = socket.getfqdn() - try: - return socket.gethostbyname_ex(hostname)[2][0] - except socket.gaierror: - import psutil - for _, addrs in psutil.net_if_addrs().items(): - for addr in addrs: - if addr.family == socket.AF_INET and not addr.address.startswith('169.254'): - return addr.address + ifaces = psutil.net_if_addrs() + # 常见的虚拟网卡名称关键词 + virtual_iface_keywords = ['vEthernet', 'VirtualBox', 'VMware', 'Hyper-V', 'Wireless', 'VPN', 'Loopback'] + + for interface_name, iface_addresses in ifaces.items(): + if any(keyword in interface_name for keyword in virtual_iface_keywords): + continue + + for iface_address in iface_addresses: + # 检查IPV4地址 + if iface_address.family == socket.AF_INET: + # 检查是否为自动获取的APIPA地址 + if not iface_address.address.startswith('169.254'): + # 返回第一个非APIPA的IPV4地址 + return iface_address.address + + return None def start_recording(self, max_time=1800, output=None, fps=10,