使用python实现登录校园网


每当下课后打开电脑联网都会跳出这个烦人的页面,每次都要手动登录,太麻烦了(

然后拿起python准备写一个登录脚本

对校园网登录表单进行抓包


我们可以看到登录时通过connect页面提交了一个表单

我们重点去看Request Headers和From Data

然后就可以根据表单信息去模拟post提交操作

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
import requests
import os
import json

#此部分是为了获得重定向链接
def connectWeb(user,password):
url = "http://1.1.1.1" #通过1.1.1.1可直接进入WeNet登录页面
res = requests.get(url,allow_redirects=False)
location = res.headers["Location"]
print("重定向到{}".format(location))
login(user,password,location)

def login(user,password,location):
url = "http://223.99.141.139:9090/web/connect"
#表单提交
data = {
"web-auth-user": user,
"web-auth-password": password,
"redirect-url": location
}
#模拟请求头
header = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Connection": "keep-alive",
"Content-Length": "217",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Cookie": "BIGipServersc-ltm-xywpt-portal3-pool=135926188.33315.0000",
"Host": "223.99.141.139:9090",
"Origin": "http://223.99.141.139:9090",
"Referer": location,
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.88 Safari/537.36",
"X-Requested-With": "XMLHttpRequest"
}
res = requests.post(url,data,headers=header)
print("回应代码: {}".format(res.status_code))

if res.status_code == 200:
print("登录成功")
os.system("pause")
else:
data = json.loads(res.text)
print("错误代码: {}\n{}".format(data["error"],data["error_description"]))
print("登录失败,请检查账号密码是否正确/是否被其它设备占用")
os.system("pause")

if __name__ == '__main__':
#输入你的账号密码
user = ""
password = ""
print("登录账号: {}".format(user))
print("登录密码: {}".format(password))
try:
connectWeb(user,password)
except BaseException as err:
print("--------错误--------")
print(str(err))
os.system("pause")

最终效果


对之前的代码新加了ini存储功能,现在不用直接在程序中修改账号密码

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
import requests
import os
import json
import configparser
import win32api
import win32con

def readInfo():
fileName = "accinfo.ini" #在软件根目录生成一个ini文件
conf = configparser.ConfigParser()
conf.read(fileName)
if os.path.exists(fileName):
user = conf.get('config', 'user')
password = conf.get('config', 'password')
if user == "" or password == "":
win32api.MessageBox(0, "你想拿空气登录吗(\n例:\n[config]\nuser = 11451419198\npassword = 123456", "错误",win32con.MB_ICONERROR)
os.system(os.getcwd()+"\\"+fileName)
readInfo()
else:
print("登录账号: {}".format(user))
print("登录密码: {}".format(password))
print("请稍等......")
connectWeb(user,password)
else:
print("配置文件错误或不存在")
print("首次启动需要提供上网账号与密码")
print("在运行目录 \"{}\" 生成 accinfo.ini".format(os.getcwd()))
conf.add_section('config')
conf.set('config', 'user', '')
conf.set('config', 'password', '')
with open(fileName, 'w') as fw:
conf.write(fw)
print("请在accinfo.ini中填写上网账号(user)与密码(password),保存并关闭")
win32api.MessageBox(0, "首次启动需要提供上网账号与密码,请在accinfo.ini中填写上网账号(user)与密码(password),保存并关闭\n例:\n[config]\nuser = 11451419198\npassword = 123456", "提示",win32con.MB_ICONASTERISK)
os.system(os.getcwd()+"\\"+fileName)
readInfo()

def connectWeb(user,password):
url = "http://1.1.1.1"
try:
res = requests.get(url,allow_redirects=False)
location = res.headers["Location"]
except BaseException as err:
print("--------错误--------")
print(str(err))
os.system("pause")
else:
print("重定向到{}".format(location))
login(user,password,location)

def login(user,password,location):
ip = "223.99.141.139:9090" #不能保证全部地区都适用
url = "http://{}/web/connect".format(ip)
data = {
"web-auth-user": user,
"web-auth-password": password,
"redirect-url": location
}
header = {
"Accept": "*/*",
"Accept-Encoding": "gzip, deflate",
"Accept-Language": "zh-CN,zh;q=0.9",
"Connection": "keep-alive",
"Content-Length": "219",
"Content-Type": "application/x-www-form-urlencoded; charset=UTF-8",
"Cookie": "BIGipServersc-ltm-xywpt-portal3-pool=135926188.33315.0000",
"Host": ip,
"Origin": "http://{}".format(ip),
"Referer": location,
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/87.0.4280.66 Safari/537.36",
"X-Requested-With": "XMLHttpRequest"
}
res = requests.post(url,data,headers=header)
print("回应代码: {}".format(res.status_code))

if res.status_code == 200:
print("登录成功")
os.system("pause")
else:
data = json.loads(res.text)
print("错误代码: {}\n{}".format(data["error"],data["error_description"]))
print("登录失败,请检查账号密码是否正确/是否被其它设备占用")
os.system("pause")

if __name__ == '__main__':
try:
print("NetKeeper山东移动高校宽带一键登录")
print("v1.1.0.210306")
readInfo()
except BaseException as err:
print("--------错误--------")
print(str(err))
os.system("pause")