How to use connected command from winwifi package

I’m trying to write a program using the connected command from the winwifi package. According to PyPi, the usage is wifi connected [SWITCHES]. And there are some examples in StackOverflow . When I try wifi connected on the command line it works. But when I try winwifi.WinWiFi.connected() in IDLE, I get this error: AttributeError: type object 'WinWiFi' has no attribute 'connected'. If I try winwifi.WinWiFi.disconnect(), however, that works (it disconnects my WiFi).

What am I doing wrong?

By John via Discussions on Python.org at 05Aug2022 03:27:

I’m trying to write a program using the connected command from the
winwifi package. According to PyPi, the usage is wifi connected [SWITCHES]. And there are some examples in
StackOverflow
. When I try wifi connected on the command line it works. But when I
try winwifi.WinWiFi.connected() in IDLE, I get this error:
AttributeError: type object 'WinWiFi' has no attribute 'connected'.
If I try winwifi.WinWiFi.disconnect(), however, that works (it
disconnects my WiFi).

What am I doing wrong?

Perhaps you need an instance of WinWiFi, not the class itself? That
would be normal - usually you always work though an instance of a class.

Test code:

# get a WinWiFi instance
wifi = winwifi.WinWiFi()
# ask it if it is connected
print(wifi.connected())

I’m not on Windows, so this is generic advice which I can’t test.

Cheers,
Cameron Simpson cs@cskk.id.au

I just tried that and got the same error.

The winwifi cli uses a method called get_connected_interfaces internally when the connected command is issued.

You can achieve the same thing like this:

from winwifi import WinWifi

interfaces = [i.ssid for i in WinWifi.get_connected_interfaces()]

(Winwifi uses a lambda in map instead of a comprehension, which is discouraged but ultimately achieves the same thing.)