问题描述
我正在尝试使用 WlanGetAvailableNetworkList
获取所有可用网络的列表.扫描返回一个包含 NumberOfItems
的对象.当我遍历基于 NumberOfItems
的网络数组时,它只向我显示第一个网络,而除此之外的任何内容都会给我 IndexError: invalid index
.这是我的代码
from win32wifi.Win32Wifi import WlanScan, WlanOpenHandle, WlanGetProfileList, WlanEnumInterfaces, WlanGetAvailableNetworkList, WlanCloseHandle, WlanConnect句柄 =WlanOpenHandle()接口 = WlanEnumInterfaces(handle).contentsg= interfaces.InterfaceInfo[0].InterfaceGuidWlanScan(手柄,g)网络 = WlanGetAvailableNetworkList(handle, g).contentsprint("网络数量:",networks.NumberOfItems)对于我在范围内(networks.NumberOfItems):打印('网络:',networks.Network[i].dot11Ssid.SSID)WlanCloseHandle(句柄)
这个问题与这个问题相关
我在评论中说得太早了(所以我删除了它).显然,win32wifi.Win32Wifi 提供了许多封装 CTypes 的功能,但是由于诸如 from win32wifi.Win32NativeWifiApi import *
之类的语句,命名空间受到了污染.无论如何,这是一个例子.
code00.py:
#!/usr/bin/env python3导入系统从 win32wifi 导入 Win32Wifi as ww定义主():接口 = ww.getWirelessInterfaces()print("WLAN 接口:{:d}".format(len(interfaces)))对于 idx0,enumerate(interfaces) 中的接口:print("\n {:d}\n GUID: [{:s}]\n Description: [{:s}]\n State: [{:s}]".format(idx0, interface.guid_string, interface.description, interface.state_string))网络 = ww.getWirelessAvailableNetworkList(接口)print("\n 网络:{:d}".format(len(networks)))对于 idx1,枚举中的网络(网络):print("\n {:d}\n SSID: [{:s}]\n Profile: [{:}]\n Connectable: {:}\n 信号质量: {:d}\n 标志: {:d}\n 安全性:{:}\n 身份验证:{:}".format(idx1、network.ssid.decode()、network.profile_name、network.connectable、network.signal_quality、network.flags、network.security_enabled、network.auth))如果 __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 如果 sys.maxsize > 0x100000000 否则 32, sys.platform))main(*sys.argv[1:])打印(\n完成.")
输出:
[cfati@CFATI-5510-0:e:\Work\Dev\StackOverflow\q056703966]>"e:\Work\Dev\VEnvs\py_pc064_03.07.06_test0\Scripts\python.exe" code00.pyPython 3.7.6 (tags/v3.7.6:43364a7ae0, Dec 19 2019, 00:42:30) [MSC v.1916 64 位 (AMD64)] 64 位在 win32WLAN 接口:10GUID:[{0C58E048-BC0B-4D5F-A21F-FCD4E4B31806}]描述:[Intel(R) Dual Band Wireless-AC 8260]状态:[wlan_interface_state_connected]网络:40SSID: [D****l 软件]简介:【D****l软件】可连接:真信号质量:91标志:3安全性:真实身份验证:DOT11_AUTH_ALGO_RSNA_PSK1SSID:[]轮廓: []可连接:真信号质量:85标志:0安全性:真实身份验证:DOT11_AUTH_ALGO_RSNA_PSK2SSID:[秒]轮廓: []可连接:真信号质量:33标志:0安全性:真实身份验证:DOT11_AUTH_ALGO_RSNA_PSK3SSID: [D****l 软件]轮廓: []可连接:真信号质量:91标志:0安全性:真实身份验证:DOT11_AUTH_ALGO_RSNA_PSK完毕.
更新#0
虽然它与这个问题无关,但我在处理 .可能想看看.
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
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:
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.
这篇关于无法在 Python 中使用 WlanGetAvailableNetworkList 获取所有可用网络的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!