python how to get an on wifi access event

Solutions on MaxInterview for python how to get an on wifi access event by the best coders in the world

showing results for - "python how to get an on wifi access event"
Tatiana
20 Jul 2019
1import socket
2import time
3import subprocess
4
5def check():#this function checks if there is an wifi access
6    try:
7        host = socket.gethostbyname("www.google.com")
8        s = socket.create_connection((host, 80), 2)
9        return True
10    except Exception:
11        return False
12
13      
14while True:#if you want to do something when you get a wifi access so use this loop
15    if check() == True:
16        DoSomeStuff()#whatever you want to do
17    else:
18        subprocess.call(["/bin/bash", "-c", "rfkill", "block", "wifi"])
19        subprocess.call(["/bin/bash", "-c", "rfkill", "unblock", "wifi"])
20        subprocess.Popen(["/bin/bash", "-c", "notify-send 'conncection re-established'"])
21    time.sleep(4)