Lemon's Blog

Lemon

Python&使用POST网页请求实现校园网自动登录

138
2024-03-07
Python&使用POST网页请求实现校园网自动登录

首先感谢 @Alx962 的分享灵感

此文章仅用于学习分享,切勿用于非法及商业用途

Support:

  • 弹窗反馈

  • 表单提交

  • 灵活获取设备ip&mac

  • 设置自启动登录

1. 检查校园网的请求方式

本文章适用于校园网的登录通过 POST 请求实现的,具体在开发者工具F12中查看数据包的请求方式:

2. 项目python源码

# 发送HTTP请求
import requests  
# 获取本机IP地址
import socket
# 生成UUID
import uuid
# 时间戳
import time
# 弹窗
import tkinter as tk


#运行结果弹窗
def show_popup():
        popup = tk.Tk()
        popup.title("校园网登录程序")
        popup.geometry("280x240")
        popup.wm_attributes("-topmost", 1)
        frame = tk.Frame(popup)
        frame.pack(pady=50)
        label = tk.Label(frame, text="Wifi自动登录程序已完成运行,可以上网了\n\n若未成功,请检查校园网是否连接!\n\n\nMail:mail@redop.cn\n联系定制↑↑↑")
        label.pack()
        popup.update_idletasks()
        width = popup.winfo_width()
        height = popup.winfo_height()
        x = (popup.winfo_screenwidth() // 2) - (width // 2)
        y = (popup.winfo_screenheight() // 2) - (height // 2)
        popup.geometry('{}x{}+{}+{}'.format(width, height, x, y))
        popup.mainloop()
    
    
    #获取设备IP
    def get_ip():
        hostname = socket.gethostname()
        ip_address = socket.gethostbyname(hostname)
        return ip_address
    
    #获取设备MAC
    def get_mac():
        mac_address = ':'.join(['{:02x}'.format((uuid.getnode() >> i) & 0xff) for i in range(0,8*6,8)][::-1])
        return mac_address
    
    #构造URL和构造数据,填法往文章下面看
    url = f'http://172.16.252.106/webauth.do?wlanacip=172.16.252.252&wlanacname=SWSM-BRAS&wlanuserip={get_ip()}&mac={get_mac()}&vlan=0&url=http://1.1.1.1'
    data = {
        "scheme": "http",
        ...
        ...
        ...
        "isBindMac": "bindmac",
    }
    
    #0.2s执行
    time.sleep(0.2)
    
    #发送HTTP请求
    conn = requests.post(url=url, data=data)
    
    #显示弹窗
    show_popup()

3. 部分代码解释及建议

  1. 根据校园网的不同url请求地址url=f'请求链接'会不同。

  2. 由于我的校园网请求地址中包含 wlanuseripmac 地址,在代码中添加了两个获取方法,若不涉及可以删除。

  3. data数据,找到 数据包-荷载-表单数据,F12打开开发者工具-点击网络(或network)。在登录框输入账号密码开始登录,你会发现”网络“一下子更新了很多数据。现在需要找到包的位置,按照格式依次完整写入,注意双引号:

  4. 运行窗口文字可任意更改,不受影响。

  5. 建议通过 .pyw 后缀运行,因为不会显示cmd窗口,影响使用

  6. 项目可打包成 .exe 具体方法请下载完整文件。

  7. 可通过计划任务实现连接校园网自动运行程序,自行百度。

4. 完整文件下载

🔽🔽含打包教程:

👉👉从蓝奏云下载

  • 1