无法使用Python中的WlanGetAvailableNetworkList获取所有可用的网络

时间:2019-06-21 13:00:06

标签: python python-3.x python-2.7 winapi pywin32

我正在尝试使用WlanGetAvailableNetworkList获取所有可用网络的列表。扫描返回一个包含NumberOfItems的对象。当我遍历基于NumberOfItems的网络阵列时,它仅显示第一个网络,而超出此范围的任何内容都会给我IndexError: invalid index。 这是我的代码

from win32wifi.Win32Wifi import WlanScan, WlanOpenHandle, WlanGetProfileList, WlanEnumInterfaces, WlanGetAvailableNetworkList, WlanCloseHandle, WlanConnect

handle =WlanOpenHandle()
interfaces = WlanEnumInterfaces(handle).contents
g= interfaces.InterfaceInfo[0].InterfaceGuid
WlanScan(handle, g)
networks= WlanGetAvailableNetworkList(handle, g).contents
print("Number of networks : ", networks.NumberOfItems)
for i in range(networks.NumberOfItems):
    print('Network : ', networks.Network[i].dot11Ssid.SSID )

WlanCloseHandle(handle)

这个问题与this question

有关

1 个答案:

答案 0 :(得分:1)

我在评论中讲得太早了(所以我删除了它)。显然,win32wifi.Win32Wifi提供了许多包装 ctypes 的功能,但是由于类似from win32wifi.Win32NativeWifiApi import *这样的语句,名称空间受到污染。无论如何,这是一个例子。

code.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)))
    for idx0, interface in enumerate(interfaces):
        print("\n  {:d}\n  GUID: [{:s}]\n  Description:  [{:s}]\n  State: [{:s}]".format(idx0, interface.guid_string, interface.description, interface.state_string))
        networks = ww.getWirelessAvailableNetworkList(interface)
        print("\n  Networks: {:d}".format(len(networks)))
        for idx1, network in enumerate(networks):
            print("\n    {:d}\n    SSID: [{:s}]\n    Profile: [{:}]\n    Connectable: {:}\n    Signal quality: {:d}\n    Flags: {:d}\n    Security: {:}\n    Auth: {:}".format(
                idx1, network.ssid.decode(), network.profile_name, network.connectable, network.signal_quality, network.flags, network.security_enabled, network.auth))


if __name__ == "__main__":
    print("Python {:s} on {:s}\n".format(sys.version, sys.platform))
    main()
    print("\nDone.")

输出

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q056703966]> "e:\Work\Dev\VEnvs\py_064_03.07.03_test0\Scripts\python.exe" code.py
Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit (AMD64)] on win32

WLAN Interfaces: 1

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

  Networks: 4

    0
    SSID: [D****l Software]
    Profile: [D****l Software]
    Connectable: True
    Signal quality: 91
    Flags: 3
    Security: True
    Auth: DOT11_AUTH_ALGO_RSNA_PSK

    1
    SSID: []
    Profile: []
    Connectable: True
    Signal quality: 85
    Flags: 0
    Security: True
    Auth: DOT11_AUTH_ALGO_RSNA_PSK

    2
    SSID: [sec]
    Profile: []
    Connectable: True
    Signal quality: 33
    Flags: 0
    Security: True
    Auth: DOT11_AUTH_ALGO_RSNA_PSK

    3
    SSID: [D****l Software]
    Profile: []
    Connectable: True
    Signal quality: 91
    Flags: 0
    Security: True
    Auth: DOT11_AUTH_ALGO_RSNA_PSK

Done.

@ EDIT0

尽管与该问题无关,但我在[SO]: How to connect to WiFi network using Python 3? (@CristiFati's answer)上工作时发现(并修复了)一些 Win32WiFi 错误。可能想看看。