在 Python 中使用 WlanScan 强制扫描 wifi

Force wifi scan using WlanScan in Python

我想知道如何从 python 执行 WlanScan 函数来启动无线网络扫描。我正在使用 python 模块 win32wifi。它需要使用 WlanOpenHandle 和接口 GUID pInterfaceGuid 获得的句柄。我不知道如何获得这个 GUID。任何帮助将不胜感激。

您通过 WlanEnumInterfaces which returns WLAN_INTERFACE_INFO_LIST structure with WLAN_INTERFACE_INFO structureInterfaceGuid 成员获得 Guid

我安装了 Win32WiFi 模块,并在简要检查了 @Castorix 提供的 URLs 之后(所有必需信息可以在 [MS.Docs]: wlanapi.h header) 和源代码中找到,我能够编写这个小示例。

code00.py:

#!/usr/bin/env python3

import sys
from win32wifi import Win32Wifi as ww


def main():
    interfaces = ww.getWirelessInterfaces()
    print("WLAN Interfaces: {:d}".format(len(interfaces)))
    handle = ww.WlanOpenHandle()
    for idx , interface in enumerate(interfaces):
        print("\n  {:d}\n  GUID: [{:s}]\n  Description: [{:s}]".format(idx, interface.guid_string, interface.description))
        try:
            scan_result = ww.WlanScan(handle, interface.guid)
        except:
            print(sys.exc_info())
            continue
        print("\n  Scan result: {:d}".format(scan_result))
    ww.WlanCloseHandle(handle)


if __name__ == "__main__":
    print("Python {0:s} {1:d}bit on {2:s}\n".format(" ".join(item.strip() for item in sys.version.split("\n")), 64 if sys.maxsize > 0x100000000 else 32, sys.platform))
    main()
    print("\nDone.")

输出:

[cfati@CFATI-5510-0:e:\Work\Dev\Whosebug\q056701614]> "e:\Work\Dev\VEnvs\py_pc064_03.07.06_test0\Scripts\python.exe" code00.py
Python 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 bit (AMD64)] 64bit on win32

WLAN Interfaces: 1

  0
  GUID: [{0C58E048-BC0B-4D5F-A21F-FCD4E4B31806}]
  Description: [Intel(R) Dual Band Wireless-AC 8260]

  Scan result: 0

Done.



更新#0

根据 更新了代码。它现在适用于具有多个 WLAN 适配器

的计算机