自动登录校园网代码加入wifi自动搜寻并主动连接部分

距离上次写完自动登录校园网的代码过去已经有一段时间了,在使用过程中,我搭配电脑的开机自启服务一块使用,感觉不错,但是也有一点小问题。

问题如下:有时候电脑开机之后并不会自动连接NJUPT-CMCC。

因为上述问题,我开机之后电脑运行会出现无法获取该DNS的信息,在我手动进行WiFi的连接之后,我的程序会卡住无法正常运行,我只能重新去点击代码运行多一次,一次不麻烦,但是次数多了,肯定会觉得麻烦。恰好今晚开机也出现这个问题,于是查找了CSDN一些大佬关于自动连接wifi的文章,并稍加修改加入旧代码当中。
参考文献:
Python之pywifi模块
python wifi模块

感谢以上两篇文章的大佬。

代码仍存在的问题:

1、代码不能解决自动打开电脑wifi开关的问题。需要电脑一直保持无线网卡的开启。
2、关于自动连接wifi模块,存在耗时较多的问题。但这个时间可以大家依据自身日常使用来调节sleep的时间,我个人觉得3+5秒比较适合。
3、新加入代码分块不明显,看着会稍累。局限于本人水平有限,就不分开过多方法了。

下面直接上代码,如果有不懂的可以翻找我之前的博客,里面都很详细,或者评论留言。

新加的代码部分:

from pywifi import PyWiFi, const, Profile

def check_wifi():
	wifi_name = "NJUPT-CMCC"
	wifi_num = 0
	
	wifi = PyWiFi()
	iface = wifi.interfaces()[0]

	if iface.status() in[const.IFACE_CONNECTED,
                          const.IFACE_CONNECTING]:
		wifi_num = 0
		print('无线网卡 %s 已连接!' % iface.name())
	else:
		wifi_num = 1
		print('无线网卡 %s 未连接!' % iface.name())
    #判断wifi未连接
	while wifi_num == 1:
		iface.scan()
		results = iface.scan_results()
		for result in results:
			if result.ssid == wifi_name:
				print('wifi名称:{0},该信号强度:{1}。'.format(result.ssid, result.signal))
				#进行连接工作
				time.sleep(3)
				profile_info = Profile()
				profile_info.ssid = wifi_name
				iface.remove_all_network_profiles()
				tmp_profile = iface.add_network_profile(profile_info)
				iface.connect(tmp_profile)
				time.sleep(5)
				if iface.status() == const.IFACE_CONNECTED: 
					print("wifi: %s 连接成功!" % wifi_name)
					wifi_num = 0
					break
				else:
					print("wifi: %s 连接失败!" % wifi_name)
					wifi_num = 1
			else:
				pass

#主程序
#判断网卡是否连接
check_wifi()

完整校园网自动登录代码如下:

#login_selenium.py
from selenium import webdriver
from selenium.webdriver.support import expected_conditions as EC
from selenium.webdriver.support.ui import WebDriverWait
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.common.by import By
from selenium.webdriver.chrome.options import Options
import time, os, requests
from pywifi import PyWiFi, const, Profile
'''
NJUPT-CMCC/ChinaNet自动登录代码
author:海hong

2021年3月31号,加入wifi自动检测自动连接功能。
使用到pywifi。
'''

def login(account, password):
	#print(account, password)
	input_account = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '#edit_body > div.edit_row.ui-resizable-autohide > div.edit_loginBox.ui-resizable-autohide > form > input:nth-child(3)')))
	input_password = wait.until(EC.presence_of_all_elements_located((By.CSS_SELECTOR, '#edit_body > div.edit_row.ui-resizable-autohide > div.edit_loginBox.ui-resizable-autohide > form > input:nth-child(4)')))
	button = driver.find_element_by_css_selector('#edit_body > div.edit_row.ui-resizable-autohide > div.edit_loginBox.ui-resizable-autohide > form > input:nth-child(2)')

	input_account[0].send_keys(account)
	input_password[0].send_keys(password)
	#print("准备点击")
	button.click()

def back():
	button_back = driver.find_element_by_css_selector('#edit_body > div > div.edit_loginBox.ui-resizable-autohide > form > input')
	button_back.click()

def quit():
	button_quit = driver.find_element_by_css_selector('#edit_body > div > div.edit_loginBox.ui-resizable-autohide > form > input')
	button_quit.click()
	time.sleep(2)
	confirm = driver.switch_to.alert
	confirm.accept()
	print('你刚刚确认下线')
	time.sleep(5)
	confirm.accept()
	print('现在返回上网登录窗页面')

def Is_OK():
	global IsCode
	#判断跳转界面
	#driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
	page_information = driver.find_element_by_css_selector('head > title').get_attribute('textContent')
	if page_information == '认证成功页':
		#判断联网
		r = requests.get('http://www.baidu.com', timeout =30)
		#print(r.status_code)
		if r.status_code == 200:
			IsCode = 0
			print("login successfully")
		else:
			IsCode = 1
			quit()
			login(account, password)
	elif page_information == '信息页':
		print("出现信息页,正在返回准备重新登录")
		back()
		login(account, password)
		IsCode = 1
	elif page_information == '上网登录窗':
		login(account, password)
		IsCode = 1
	return IsCode

def Is_page():
	#判断跳转界面
	global IsCode
	#driver.execute_script("window.scrollTo(0, document.body.scrollHeight);")
	page_information = driver.find_element_by_css_selector('head > title').get_attribute('textContent')

	if page_information == '上网登录窗':
		login(account, password)

	elif page_information == '信息页':
		back()
		login(account, password)

	elif page_information == '认证成功页':
		r = requests.get('http://www.baidu.com', timeout =30)

		if r.status_code == 200:
			IsCode = 0
			print("已登陆成功,无需重复登陆")
		else:
			quit()
			login(account, password)

	
	#检测页面是否跳转到认证成功页&能否正常上网
	count = 0
	while IsCode != 0:
		count = count + 1
		Is_OK()
		if IsCode == 1:
			print("第%s次验证无法通过"%str(count))
		else:
			print("第%s次验证通过"%str(count))
		if count == 4:
			print("验证次数过多,请注意是否断网或者账号密码出错!")
			break

def check_wifi():
	wifi_name = "NJUPT-CMCC"
	wifi_num = 0
	
	wifi = PyWiFi()
	iface = wifi.interfaces()[0]

	if iface.status() in[const.IFACE_CONNECTED,
                          const.IFACE_CONNECTING]:
		wifi_num = 0
		print('无线网卡 %s 已连接!' % iface.name())
	else:
		wifi_num = 1
		print('无线网卡 %s 未连接!' % iface.name())
    #判断wifi未连接
	while wifi_num == 1:
		iface.scan()
		results = iface.scan_results()
		for result in results:
			if result.ssid == wifi_name:
				print('wifi名称:{0},该信号强度:{1}。'.format(result.ssid, result.signal))
				#进行连接工作
				time.sleep(3)
				profile_info = Profile()
				profile_info.ssid = wifi_name
				iface.remove_all_network_profiles()
				tmp_profile = iface.add_network_profile(profile_info)
				iface.connect(tmp_profile)
				time.sleep(5)
				if iface.status() == const.IFACE_CONNECTED: 
					print("wifi: %s 连接成功!" % wifi_name)
					wifi_num = 0
					break
				else:
					print("wifi: %s 连接失败!" % wifi_name)
					wifi_num = 1
			else:
				pass

#主程序
#判断网卡是否连接
check_wifi()

url = 'http://p.njupt.edu.cn'
#设置无头模式
chrome_options = Options()
chrome_options.add_argument('--headless')
driver = webdriver.Chrome(options = chrome_options)
#driver = webdriver.Chrome()
wait = WebDriverWait(driver, 30)
driver.get(url)
account = 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX'
password = 'XXXXXX'
IsCode = 1

Is_page()

driver.quit()
print("5秒后窗口自动关闭")
time.sleep(4)
os.sys.exit()

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值