自动连接需要登录认证的WIFI和切换电脑模式

1、有的WIFI在使用时需要进入登录网址输入账号和密码,并且长时间未操作电脑后网络会掉线需进入登录网址才能重新连接互联网,本代码旨在解决此问题。

2、在规定的时间段内切换电脑模式为预先设定的模式,以联想拯救者Y7000P2023款为例,自由切换电脑模式为安静、均衡、野兽和超能模式,如在00:00:00~08:00:00这段时间为野兽模式,在08:00:01~23:59:59又自动切换为均衡模式,而无需手动打开Legion Zone软件手动切换或按键盘快捷键切换。

从上可看出这是为了解决远程操作电脑而生的,如果电脑就在你面前当然就无需这些了,远程就怕连接了WIFI却又掉网了,而本代码能确保长时间未操作电脑网络掉线后能迅速重新自动接入互联网。

import os
import webbrowser
import pyautogui
import time
from datetime import datetime
from ultralytics.utils import LOGGER, colorstr
import traceback
import socket


def is_connected():
    try:
        socket.create_connection(("8.8.8.8", 53), timeout=5)
        return True
    except OSError:
        return False


def move_and_click(x, y, times=1):
    pyautogui.moveTo(x, y, times)
    time.sleep(1)
    pyautogui.click()
    time.sleep(1)


def main():
    LOGGER.info(f"{colorstr('green', '****************************************')}\n{colorstr('red', 'WARNING:')} {colorstr('blue', 'Must run in administrator mode!')}\n{colorstr('green', '****************************************')}")
    if not open_switch_computer_mode:
        LOGGER.info(f"{colorstr('red', 'Computer mode switching not enabled.')}")
    if not open_log_in_to_campus_network:
        LOGGER.info(f"{colorstr('red', 'Login to campus network not enabled.')}")
    compute_mode = initial_compute_mode
    while True:
        try:
            if open_log_in_to_campus_network:
                if not is_connected():
                    LOGGER.info(f"{colorstr('red', 'The network is not connected, try opening the login interface.')}")
                    webbrowser.open(url)
                    time.sleep(3)
                    if not is_connected():
                        LOGGER.info(f"{colorstr('red', 'The network is still not connected, try to automatically enter the account and password to log in.')}")
                        move_and_click(1428, 372)
                        pyautogui.typewrite('登录WIFI时的账号', interval=0.25)
                        time.sleep(1)
                        pyautogui.click(1614, 404)
                        move_and_click(1428, 409)
                        pyautogui.typewrite('登录WIFI时的密码', interval=0.25)
                        time.sleep(1)
                        pyautogui.click(1614, 443)
                        move_and_click(1555, 449)
                        move_and_click(1428, 490)
                        move_and_click(1298, 484)
                        move_and_click(1507, 484)
                        move_and_click(1430, 542)
                    move_and_click(1895, 13)
                    pyautogui.moveTo(960, 540)
                    time.sleep(1)
                    LOGGER.info(f"{colorstr('red', 'The time to log in to the campus network is')} {colorstr('blue', datetime.now().strftime('%Y-%m-%d %H:%M:%S'))}.")
            if open_switch_computer_mode:
                now = datetime.now()
                if datetime(now.year, now.month, now.day, 0, 0, 0) <= now <= datetime(now.year, now.month, now.day, 8, 0, 0) and compute_mode != 'beast':
                    os.system(legionzone_path)
                    time.sleep(2)
                    move_and_click(947, 574)
                    move_and_click(1441, 212)
                    pyautogui.moveTo(960, 540)
                    time.sleep(1)
                    compute_mode = 'beast'
                    LOGGER.info(f"{colorstr('red', 'Beast mode has been activated. Current time is')} {colorstr('blue', now.strftime('%Y-%m-%d %H:%M:%S'))}.\n")
                if datetime(now.year, now.month, now.day, 8, 0, 0) < now <= datetime(now.year, now.month, now.day, 23, 59, 59) and compute_mode == 'beast':
                    os.system(legionzone_path)
                    time.sleep(2)
                    if initial_compute_mode == 'quiet':
                        move_and_click(787, 572)
                        move_and_click(1441, 212)
                        pyautogui.moveTo(960, 540)
                        time.sleep(1)
                        compute_mode = 'quiet'
                        LOGGER.info(f"{colorstr('red', 'Quiet mode has been activated. Current time is')} {colorstr('blue', now.strftime('%Y-%m-%d %H:%M:%S'))}.\n")
                    else:
                        move_and_click(868, 576)
                        move_and_click(1441, 212)
                        pyautogui.moveTo(960, 540)
                        time.sleep(1)
                        compute_mode = 'balanced'
                        LOGGER.info(f"{colorstr('red', 'Balanced mode has been activated. Current time is')} {colorstr('blue', now.strftime('%Y-%m-%d %H:%M:%S'))}.\n")
        except:
            LOGGER.info(f"{colorstr('red', '⚠ERROR: Unexpected Error.')}")
            time.sleep(1)
            traceback.print_exc()
        time.sleep(check_interval_time)


if __name__ == '__main__':  # 如果是在PyCharm中运行此代码,那么打开PyCharm时一定要选择以管理员身份运行,否则有些与pyautogui相关的语句会失效
    check_interval_time = 1  # 每隔多少秒就检查电脑是否连接互联网
    open_switch_computer_mode = True  # 是否开启在指定时间段切换到预设的电脑模式(此代码适配的是联想拯救者Y7000P2023款的安静、均衡、野兽和超能模式)
    open_log_in_to_campus_network = True  # 是否开启自动连接需要进行登录认证的WIFI
    initial_compute_mode = 'balanced'  # 期望在某个时间段切换成此种电脑模式,quiet=(787, 572), balanced=(868, 576), beast=(947, 574), hyper=(1027, 575)
    url = 'http://202.203.158.252/'  # 需要进行登录认证的WIFI对应的登录网址
    legionzone_path = r"D:\legion_zone\LegionZone\LZMain.exe"  # 联想Legion Zone软件对应的可执行文件的路径
    main()

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值