How to connect WiFi using Python?

Python provides several libraries for automating WiFi connections, which is useful for headless systems or automated network management. The wifi library offers a simple approach to scan and connect to wireless networks programmatically.

Prerequisites

Before starting, install the required library ?

pip install wifi

Note: This library works primarily on Linux systems and requires appropriate system permissions for network management.

Using the wifi Library

The wifi library provides classes to scan networks and manage connections. Here are the key steps ?

  • Import the wifi library in your Python script

  • Use the Cell class to scan for available WiFi networks

  • Select the network and provide authentication credentials using the Scheme class

  • Use the scheme methods to save and activate the connection

Complete Example

import wifi
import time

try:
    # Scan for available WiFi networks
    wifi_scanner = wifi.Cell.all('wlan0')
    available_networks = [cell.ssid for cell in wifi_scanner]
    
    # Print available networks
    print(f"Available Networks: {available_networks}")
    
    # Connect to a WiFi network
    network_ssid = input("Enter network SSID: ")
    network_pass = input("Enter network password: ")
    
    for cell in wifi_scanner:
        if cell.ssid == network_ssid:
            scheme = wifi.Scheme.for_cell('wlan0', cell.ssid, cell, network_pass)
            scheme.save()
            scheme.activate()
            print(f"Connected to network: {network_ssid}")
            break
    else:
        print(f"Unable to find network: {network_ssid}")
        
except Exception as e:
    print(f"Error: {e}")
    print("Make sure you have proper permissions and wifi interface is available")

How It Works

The code performs the following operations ?

  • wifi.Cell.all('wlan0') scans for all available networks on the specified interface

  • wifi.Scheme.for_cell() creates a connection scheme with network credentials

  • scheme.save() saves the network configuration to the system

  • scheme.activate() activates the connection

Alternative Approach with pywifi

For cross-platform compatibility, consider using pywifi library ?

import pywifi
import time

def connect_wifi(ssid, password):
    wifi = pywifi.PyWiFi()
    iface = wifi.interfaces()[0]
    
    # Disconnect from current network
    iface.disconnect()
    time.sleep(1)
    
    # Create new profile
    profile = pywifi.Profile()
    profile.ssid = ssid
    profile.auth = pywifi.const.AUTH_ALG_OPEN
    profile.akm.append(pywifi.const.AKM_TYPE_WPA2PSK)
    profile.cipher = pywifi.const.CIPHER_TYPE_CCMP
    profile.key = password
    
    # Connect
    iface.remove_all_network_profiles()
    tmp_profile = iface.add_network_profile(profile)
    iface.connect(tmp_profile)
    
    # Check connection status
    time.sleep(3)
    if iface.status() == pywifi.const.IFACE_CONNECTED:
        return True
    return False

Key Points

  • Both libraries require administrative privileges on most systems

  • Network interface names may vary (wlan0, wlp3s0, etc.)

  • Connection success depends on signal strength and correct credentials

  • Always handle exceptions for network-related operations

Conclusion

Python's wifi and pywifi libraries provide programmatic WiFi management capabilities. The wifi library works well on Linux systems, while pywifi offers better cross-platform support for automated network connections.

---
Updated on: 2026-03-27T11:08:49+05:30

8K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements