So I made this little script in python that when the individual would connect to the WiFi it would tell me about their connection I am planning to write commands when someone connects however when I have my phone unlocked it follows the try and says the phone is connected printing "up". However, when I lock my phone it turns to the except and prints down. I assume this is because packets aren't being sent when my phone is locked but I don't know I am still pretty new to programming in some aspects, are there any suggestions on how to fix this? Thanks
import nmap
import time
nm = nmap.PortScanner()
Status = "up"
while 1:
try:
nm.scan("192.168.1.160", "22")
Status = nm["192.168.1.160"].state()
except:
Status = "down"
print(Status)
time.sleep(5)