Last active
February 8, 2024 15:22
-
-
Save taylor224/516de7dd0b707bc0b1b3 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') |
Thank you very much for reply
Regards,
Chetanya Agrawal
…On Tue, Jul 6, 2021 at 9:08 PM Vincent Bénet ***@***.***> wrote:
***@***.***Benet* commented on this gist.
------------------------------
Struck Here Some Help Please
cells = wifi.Cell.all('wlan0')
Traceback (most recent call last):
File "", line 1, in
File "G:\Python\lib\site-packages\wifi\scan.py", line 38, in all
iwlist_scan = subprocess.check_output(['/sbin/iwlist', interface, 'scan'],
File "G:\Python\lib\subprocess.py", line 411, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
File "G:\Python\lib\subprocess.py", line 489, in run
with Popen(*popenargs, **kwargs) as process:
File "G:\Python\lib\subprocess.py", line 854, in *init*
self._execute_child(args, executable, preexec_fn, close_fds,
File "G:\Python\lib\subprocess.py", line 1307, in _execute_child
hp, ht, pid, tid = _winapi.CreateProcess(executable, args,
FileNotFoundError: [WinError 2] The system cannot find the file specified
It is because you are on windows 10.
I am looking for the interface to enter for windows 10 too ...
did you found any solution?
Yes:
Here you are (old code that worked a long time ago^^)
def get_current_wifi_windows():
network_information = str(subprocess.check_output(["netsh", "wlan", "show", "network"]))
network_information = network_information.replace('\\r', '')
network_information = network_information.replace("b' ", '')
network_information = network_information.replace(":", '\n')
network_information = network_information.replace("\\n", '\n')
network_information = network_information.splitlines()
ssid = network_information[6][1:]
return ssid
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<https://gist.github.com/516de7dd0b707bc0b1b3#gistcomment-3804115>, or
unsubscribe
<https://github.com/notifications/unsubscribe-auth/AISMZY4GE7RAXIV6CMKLJWLTWMPOTANCNFSM4HMF7OQA>
.
Hi, I am getting this error. What may be wrong?
[Cell(ssid=mySSID)]
Traceback (most recent call last):
File "/home/pi/wifi_con.py", line 89, in <module>
print(Connect('mySSID', '@key12345'))
File "/home/pi/wifi_con.py", line 34, in Connect
savedcell.activate()
File "/home/pi/.local/lib/python3.9/site-packages/wifi/scheme.py", line 172, in activate
subprocess.check_output(['/sbin/ifdown', self.interface], stderr=subprocess.STDOUT)
File "/usr/lib/python3.9/subprocess.py", line 424, in check_output
return run(*popenargs, stdout=PIPE, timeout=timeout, check=True,
File "/usr/lib/python3.9/subprocess.py", line 528, in run
raise CalledProcessError(retcode, process.args,
subprocess.CalledProcessError: Command '['/sbin/ifdown', 'wlan0']' returned non-zero exit status 1.
Guys…can you give me pls an answer of this question…my devise is not rooted..
Can this script run without any problems ???
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Yes:
Here you are (old code that worked a long time ago^^)
To make it work on windows 10 just use:
subprocess.check_output(["netsh", "wlan", "show", "network"])