Scan Nearby Wi-Fi Networks with Python and pywifi
A short Python script that scans for nearby Wi-Fi networks with the pywifi library and prints each network’s SSID, BSSID, signal strength, frequency, and security type.
Python projects · Wireless networking
Start here
Read the requirements and any configuration below, then follow the usage example. Source paths below are relative to the repository root; run commands from the working directory stated in Usage. This example’s source is in Wifi Scanner/Console_Code/. Install any third-party packages in an isolated Python environment.
Overview
This script initializes the system’s first Wi-Fi interface, triggers a scan, and then reads the results to display network details including SSID, BSSID, signal strength, frequency, and security mode for each discovered access point.
Features
- Scans all visible Wi-Fi networks
- Displays for each network:
- SSID (network name)
- BSSID (access point MAC address)
- Signal strength (dBm)
- Frequency (MHz)
- AKM security mode (OPEN, WPA-PSK, WPA2-PSK, WPA-EAP, WPA2-EAP, UNKNOWN)
- Authentication mode
- Error handling inside the scan/output block; interface initialization happens before that block
Known issues
- Authentication value is scalar. The upstream Profile definition stores
authas an integer, while this script indexesnetwork.auth[0]. The default0skips the branch; a nonzero integer raises a type error and ends the scan/output block. Use the documented integer constants when adapting the display rather than treating authentication as a list. - Security labels are shifted.
pywifinumbers its AKM types as 0 = none, 1 = WPA, 2 = WPA-PSK, 3 = WPA2, 4 = WPA2-PSK, 5 = unknown, but the script’sakm_modeslist is orderedOPEN, WPA-PSK, WPA-EAP, WPA2-PSK, WPA2-EAP, UNKNOWN. As a result, a typical WPA2-Personal network (type 4) is printed asWPA2-EAP. Reordering the list to['OPEN', 'WPA-EAP', 'WPA-PSK', 'WPA2-EAP', 'WPA2-PSK', 'UNKNOWN']matchespywifi’s constants. - Frequency units. On Windows,
pywifireturns the channel centre frequency in kHz, sonetwork.freq / 1000000prints gigahertz (for example2.412) under a “MHz” label. Dividing by1000gives MHz. Other platforms may report frequency in different units.
Prerequisites and installation
pywifi
Install the dependency:
python -m pip install pywifi
On Windows,
pywifimay also requirecomtypes. Install with:pip install pywifi comtypes
Usage
From the Console_Code folder:
python Wifi_Scanner.py
Example Output
Illustrative Windows output for a WPA2-Personal network, including the existing unit and security-label defects described in known issues. Actual values depend on the adapter and scan. With the default integer authentication value 0, no Security Auth line is printed.
SSID: HomeNetwork
BSSID: AA:BB:CC:DD:EE:FF
Signal Strength (dBm): -55
Frequency (MHz): 2.412
Security Mode: WPA2-EAP
Architecture
Wifi Scanner/
└── Console_Code/
└── Wifi_Scanner.py # Main script
Troubleshooting and limitations
- If no Wi-Fi interface exists,
interfaces()[0]fails before thetryblock and its friendly error handler cannot run. - The script always uses the first Wi-Fi interface (
interfaces()[0]). Systems with multiple Wi-Fi adapters will use whichever is first in the list. pywifisupport varies by platform. It works on Windows and Linux; macOS support is limited.- On Linux, you may need to run the script with
sudofor full scan access. scan()starts an asynchronous scan, and the script readsscan_results()immediately afterwards. Waiting a few seconds between the two calls (for exampletime.sleep(3)) can return a more complete list.
Related documentation
- WiFi Radar — live Wi-Fi dashboard
- WiFi Profiles — saved Windows Wi-Fi networks
- Internet Speed Test
- Back to all Python projects
Documentation links
Read all Python Projects and Code Examples guides online · Project overview · All developer projects