Managing WiFi networks¶
Discovering networks¶
You can use this library to scan for networks that are available in the air. To get a list of the different cells in the area, you can do
>>> from wifi import Cell, Scheme
>>> Cell.all('wlan0')
This returns a list of Cell
objects. Under the hood, this calls iwlist scan and parses the unfriendly output.
Each cell object should have the following attributes:
ssid
signal
quality
frequency
bitrates
encrypted
channel
address
mode
For cells that have encrypted
as True, there will also be the following attributes:
encryption_type
Note
Scanning requires root permission to see all the networks. If you are not root, iwlist only returns the network you are currently connected to.
Connecting to a network¶
In order to connect to a network, you need to set up a scheme for it.
>>> cell = Cell.all('wlan0')[0]
>>> scheme = Scheme.for_cell('wlan0', 'home', cell)
>>> scheme.save()
>>> scheme.activate()
Once you have a scheme saved, you can retrieve it using Scheme.find()
.
>>> scheme = Scheme.find('wlan0', 'home')
>>> scheme.activate()
Note
Activating a scheme will disconnect from any other scheme before connecting.
You must be root to connect to a network. Wifi uses ifdown and ifup to connect and disconnect.
-
class
wifi.
Cell
¶ Presents a Python interface to the output of iwlist.
-
classmethod
all
(interface)¶ Returns a list of all cells extracted from the output of iwlist.
-
classmethod
from_string
(cell_string)¶ Parses the output of iwlist scan for one cell and returns a Cell object for it.
-
classmethod
-
class
wifi.
Scheme
(interface, name, options=None)¶ Saved configuration for connecting to a wireless network. This class provides a Python interface to the /etc/network/interfaces file.
-
activate
()¶ Connects to the network as configured in this scheme.
-
classmethod
all
()¶ Returns an generator of saved schemes.
-
delete
()¶ Deletes the configuration from the
interfaces
file.
-
classmethod
for_cell
(interface, name, cell, passkey=None)¶ Intuits the configuration needed for a specific
Cell
and creates aScheme
for it.
-
classmethod
for_file
(interfaces)¶ A class factory for providing a nice way to specify the interfaces file that you want to use. Use this instead of directly overwriting the interfaces Class attribute if you care about thread safety.
-
save
()¶ Writes the configuration to the
interfaces
file.
-