I have two WiFi (wlan) on one raspberry pi and would like to look up and see if another RPI WIFI is working. I came across this code and I have changed some of it. I would like to choose which WiFi (wlan) I am using with out changing the whole base code.
Is there a way to make a class that will do that for this type of code and how would it look like?
I have read a bit on OOP (class) mostly about cars, dogs and employees I have not been successful at all in making a class.
Code: Select all
#!/usr/bin/python3 # -*- coding: utf-8 -*- import wifi from time import sleep def Search(Wlan = 'wlan0'): wifilist =  cells = wifi.Cell.all(Wlan) for cell in cells: wifilist.append(cell) return wifilist def FindFromSearchList(ssid): wifilist = Search() for cell in wifilist: if cell.ssid == ssid: return cell pass return False def FindFromSavedList(ssid, Wlan = 'wlan0'): cell = wifi.Scheme.find(Wlan, 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 pass return False def Add(cell, password=None, Wlan = 'wlan0'): if not cell: return False scheme = wifi.Scheme.for_cell(Wlan, 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 i = 0 for search in Search(): print() print(search.ssid) while True: try: print(Connect(search.ssid)) except Exception as e: sleep(1) i += 1 #print(e) if i >= 5: i = 0 break else: break # Connect WiFi with password & without password print() #print(Connect('OpenWiFi')) #print(Connect('ClosedWiFi', 'password')) # Delete WiFi from auto connect list #print(Delete('DeleteWiFi'))