我想知道如何让Python脚本确定计算机是如何连接到网络的。之所以如此,是因为如果连接速度太慢,脚本就会出错,所以如果用户是通过WiFi连接的,我想给出一个警告。
实际运行的Python可执行文件位于我公司的LAN上,因此连接速度很重要。此外,由于这个可执行文件不能在没有连接的情况下运行,所以我不关心检查用户是否联机。
我不知道Python是如何与这样的事情交互的(甚至可能的话),所以请告诉我是否有什么我可以澄清的。提前感谢
我在Windows764位上使用Python2.7.6 32位(因为pyEnchant不同)
发布于 2022-07-21 22:03:27
使用WMI,您可以确定连接,而不需要本地机器IP。
使用NetConnectionID而不是AdapterType进行比较似乎是可行的。
在Windows 10上工作。
import wmi
w = wmi.WMI()
for nac in w.Win32_NetworkAdapterConfiguration():
if nac.IPEnabled:
idx = nac.InterfaceIndex
break
else:
print("oops, do something, no NIC has that address")
na = w.Win32_NetworkAdapter(InterfaceIndex=idx)
if not na:
print("oops, unlikely race condition, NIC disabled right before we checked it")
if 'Wi' in na[0].NetConnectionID:
print("do WiFi stuff")
elif 'Ethernet' in na[0].NetConnectionID:
print("do Ethernet stuff")
else:
print("who's using LocalTalk in 2013?")https://stackoverflow.com/questions/20766191
复制相似问题