问题描述
我想编写一个脚本,它会不断尝试连接到网络,即使它不可用,以便一旦可用,它就会连接到它。 这就是我现在拥有的
def connect_to_yeelight(ssid,iface):
sys.setrecursionlimit(2000)
iface_channel = f"sudo iwconfig {iface} channel 6"
os.system(iface_channel)
connect_yeelight_cmd = f"nmcli d wifi connect {ssid} ifname {iface} > /dev/null 2>&1"
def try_connection():
if os.system(connect_yeelight_cmd) != 0:
try_connection()
time.sleep(1)
else:
return True
try_connection()
正如您可能使用此代码所知道的那样,我收到了“RecursionError:相比之下超出了最大递归深度”。有没有其他方法可以实现这样的脚本,我觉得我从错误的角度看这个。
解决方法
cmd = ["nmcli","-f","SSID,BSSID,ACTIVE","dev","wifi","list"]
networks = subprocess.Popen(cmd,stdout=subprocess.PIPE)
output,errors = networks.communicate()
print(output.decode("utf-8"))
如果您的 Wi-Fi 在此列表中联机,这将返回所有活动的 Wi-Fi,您可以尝试连接它。