-
-
Save AnjaneyuluBatta505/04da1f62be55fb760a764f5a4c4b80a5 to your computer and use it in GitHub Desktop.
Python WiFi Example
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import wifi | |
def Search(): | |
wifilist = [] | |
cells = wifi.Cell.all('wlan0') | |
for cell in cells: | |
wifilist.append(cell) | |
return wifilist | |
def FindFromSearchList(ssid): | |
wifilist = Search() | |
for cell in wifilist: | |
if cell.ssid == ssid: | |
return cell | |
return False | |
def FindFromSavedList(ssid): | |
cell = wifi.Scheme.find('wlan0', ssid) | |
if cell: | |
return cell | |
return False | |
def Connect(ssid, password=None): | |
cell = FindFromSearchList(ssid) | |
if cell: | |
savedcell = FindFromSavedList(cell.ssid) | |
# Already Saved from Setting | |
if savedcell: | |
savedcell.activate() | |
return cell | |
# First time to conenct | |
else: | |
if cell.encrypted: | |
if password: | |
scheme = Add(cell, password) | |
try: | |
scheme.activate() | |
# Wrong Password | |
except wifi.exceptions.ConnectionError: | |
Delete(ssid) | |
return False | |
return cell | |
else: | |
return False | |
else: | |
scheme = Add(cell) | |
try: | |
scheme.activate() | |
except wifi.exceptions.ConnectionError: | |
Delete(ssid) | |
return False | |
return cell | |
return False | |
def Add(cell, password=None): | |
if not cell: | |
return False | |
scheme = wifi.Scheme.for_cell('wlan0', cell.ssid, cell, password) | |
scheme.save() | |
return scheme | |
def Delete(ssid): | |
if not ssid: | |
return False | |
cell = FindFromSavedList(ssid) | |
if cell: | |
cell.delete() | |
return True | |
return False | |
if __name__ == '__main__': | |
# Search WiFi and return WiFi list | |
print Search() | |
# Connect WiFi with password & without password | |
print Connect('OpenWiFi') | |
print Connect('ClosedWiFi', 'password') | |
# Delete WiFi from auto connect list | |
print Delete('DeleteWiFi') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment