使用python实现登录校园网

提醒:本文编写于 112 天前,最后更新于 43 天前,其中某些信息可能已经过时

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

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

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


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

我们重点去看Request Headers和From Data

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

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存储功能,现在不用直接在程序中修改账号密码

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")
点赞

发表评论

电子邮件地址不会被公开。必填项已用 * 标注