问题
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.
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.")
Output:
[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:
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.
来源:https://stackoverflow.com/questions/56703966/unable-to-get-all-available-networks-using-wlangetavailablenetworklist-in-python