Cpywpa is another simple tool to control wpa_supplicant for Python. However, rather than using d-Bus, it wrote with Cython so it can directly use OFFICIAL C interface.
English | 简体中文
First, make sure you have the latest pip
python3 -m pip install --upgrade pip
Then you can install Cpywpa with this command
python3 -m pip install Cpywpa
Here is the dependent packages and they will be installed during installation.
If you don't want to keep them, you can remove them after installation.
package name | version |
---|---|
setuptools | any |
wheel | any |
Cython | any |
⚠ NOTE ⚠
- While only root user can access wpa_supplicant interface, all codes below are running with sudo or by root user.
- all network configuration will be saved in /etc/wpa_supplicant/wpa_supplicant.conf, while the password is saved without encryption, it is not recommended to use this on your important computer.
And here is the guide.
- Get current network status
from Cpywpa import NetworkManager
from pprint import pprint
manager = NetworkManager()
pprint(manager.getStatus())
- List known network
from Cpywpa import NetworkManager
from pprint import pprint
manager = NetworkManager()
pprint(manager.listNetwork())
- Scan network around and get scan results
from Cpywpa import NetworkManager
from pprint import pprint
from time import sleep
manager = NetworkManager()
# you can use scan() to scan and get scan results
# use scan_time to set sleep time
pprint(manager.scan(scan_time=8))
# or use onlyScan() to scan and use scanResults() to get results
manager.onlyScan()
sleep(10)
pprint(manager.scanResults())
- Connect to a network
from Cpywpa import NetworkManager
manager = NetworkManager()
# connect to a known network
# Syize is my wifi name
manager.connect('Syize')
# connect to a new network
# This new network must exist
manager.connect('Syize', passwd='wifi-password')
- Add a network but don't connect
from Cpywpa import NetworkManager
manager = NetworkManager()
manager.addNetwork('Syize', 'wifi-password')
- Delete a network
from Cpywpa import NetworkManager
manager = NetworkManager()
manager.removeNetwork('Syize')
- Chinese Wi-Fi name can show correctly in scan and scanResults, but add Wi-Fi with Chinese name HASN'T BEEN TESTED YET. Unexpected problems may occur.
- While wpa_supplicant is cross-plantform, different gcc's macro is required during installation. But till now only Linux version has been tested, and I only add Linux's macro to setup.py. It will be great if you help me completely complete this program.
- For now, Cpywpa only supportes several parameters including ssid, psk, priority and key_mgmt. I'm going to add other parameters support. However I merely use them. So it is diffcult to say when I will add.