Unable to get all available networks using WlanGet

2020-04-21 06:05发布

问题:

I am trying to get the list of all available networks using WlanGetAvailableNetworkList. The scan returns an object which contains NumberOfItems. When I loop over the array of networks based NumberOfItems it only shows me the first network and anything beyond that gives me IndexError: invalid index. here's my code

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 questions is related to this question

回答1:

I spoke too soon in my comment (so I deleted it). Apparently, win32wifi.Win32Wifi offers lots of functionalities that wrap CTypes, but the namespace is polluted because of statements like from win32wifi.Win32NativeWifiApi import *. Anyway, here's an example.

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)))
    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 {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(*sys.argv[1:])
    print("\nDone.")

Output:

[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q056703966]> "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]
  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.



Update #0

Although it's not related to this question, I found (and fixed) some Win32WiFi bugs while working on [SO]: How to connect to WiFi network using Python 3? (@CristiFati's answer). Might want to take a look.