Answers for "python - how to get an on wifi access event"

1

python - how to get an on wifi access event

import socket
import time
import subprocess

def check():#this function checks if there is an wifi access
    try:
        host = socket.gethostbyname("www.google.com")
        s = socket.create_connection((host, 80), 2)
        return True
    except Exception:
        return False

      
while True:#if you want to do something when you get a wifi access so use this loop
    if check() == True:
        DoSomeStuff()#whatever you want to do
    else:
        subprocess.call(["/bin/bash", "-c", "rfkill", "block", "wifi"])
        subprocess.call(["/bin/bash", "-c", "rfkill", "unblock", "wifi"])
        subprocess.Popen(["/bin/bash", "-c", "notify-send 'conncection re-established'"])
    time.sleep(4)
Posted by: Guest on January-12-2021

Code answers related to "python - how to get an on wifi access event"

Python Answers by Framework

Browse Popular Code Answers by Language