How to login campus network gracefully with Python?


The idea of today's article comes from the campus network. Many campus networks need to enter a website to log in every time they connect. This step is really very troublesome (college students know ~). So this time I'll teach you how to log in to the campus network with one click.

Many people learn python and don't know where to start.
After learning python and mastering the basic grammar, many people don't know where to find cases.
Many people who have done cases do not know how to learn more advanced knowledge.
So for these three types of people, I will provide you with a good learning platform, free video tutorials, e-books, and the source code of the course!
Group q96169


One click login to campus network

First, let's look at the whole process. First, you need to make a network connection. After connecting, you will jump to a website, that is, the website of campus network login.


Then enter the account and password to log in.

The effect we need to do is to click an executable file and then realize the automatic login of the campus network. (the executable file here is excu file in mac and exe in win)

We first solve how to log in through Python, and then solve the problem of packaging.

In fact, for Python login, crawler friends should soon know that they can submit the form directly through requests.

First, we capture the package to obtain the address of the campus network login submission form:


At the same time, we also get the form format that needs to be submitted. The campus network of some schools may need to be verified by the verification code. Generally, there is no very complex verification method. If verification is needed, we need to crack the verification code, . Here is the verification code cracking case I wrote before.


It's easy to do after you get it. Just post the simulation submission directly. Here I give the code I wrote. The basic logic is almost the same. You just need to change it to the relevant information of your own school.

# encoding: utf-8 
import requests 
#Check whether it is connected to the campus network
def checkinternet(): 
    url = 'xxxxxxxxxxxxxxxxxxxxxx'  #The login address of the campus network is used to test whether it is connected to the campus network
        code = requests.get(url, timeout=5).status_code 
        if code != 200: 
            print('There is no Internet~') 
            return 0 
        elif code == 200: 
            return 1 
            return 2 
        return 2 
def login(username, password): 
    url = 'xxxxxxxxxxxxxxxxxx' #Campus network form submission url
        'action': 'login', 
        'ac_id': 1, 
        'username': username, 
        'password': password, 
        'save_me': '0', 
        'ajax': 1 
    res =, data=postdata) 
    res.encoding = res.apparent_encoding 
    res = res.text 
    if 'login ok' in res:  #If this field exists
        res = "Login succeeded!" 
        return res 
    if '5 minute' in res:  #Multiple incorrect submissions may cause the account to be locked for 5 minutes
        print("Login failed:") 
        return res 
    if 'E2620' in res:   #Here is the case where the account has been logged in on another device. Log out first and then log in again
        logout(url, username, password) 
        return login(username, password) 
    return res 
#Log out of the current account
def logout(url, username, password): 
    logout_data = { 
        'action': 'logout', 
        'username': username, 
        'password': password, 
        'ajax': 1 
    res1 =, data=logout_data) 
    res1.encoding = res1.apparent_encoding 
    res1 = res1.text 
    print("Account has exited!") 
if __name__ == '__main__': 
    status = checkinternet() 
    if status == 0: 
        txt = login(xxxxx, xxxxxxxx) #Enter account password
    elif status == 1: 
        print("There is a network!") 
        txt = login(xxxxxxx, xxxxxxx) 
        print("Unknown result!") 

After explaining the code part, how can we package it into an executable file and connect to the network directly by clicking?

Here is a recommended Library -- pyinstaller

  1. pip install pyinstaller

This library can directly package py files into executable files, and supports win/mac/linux platforms.

When we finish writing the file, execute the following command:

  1. pyinstaller -F file name py

After execution, we can see that several folders have been generated, and the executable file is in the dist folder.




Put it on the desktop, and each time you connect, you can directly click to connect to the campus network~


Tags: Python

Posted by silvermice on Sat, 14 May 2022 17:20:41 +0300