Backend Development 4 min read

Python Script to Keep Campus Network Connection Alive

This article explains how to use a Python script that continuously pings an external address and automatically logs into a campus web authentication portal via HTTP POST when the connection drops, ensuring the computer stays online for remote access, and provides the full source code and setup instructions.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
Python Script to Keep Campus Network Connection Alive

Background : The campus network uses a web‑based login that requires manual entry of username and password, and the session expires after about a day, causing frequent disconnections.

Idea : Create a Python script that runs in an infinite loop, pings 8.8.8.8 to check connectivity, and when the ping fails, automatically performs a POST request to the login URL using stored (Base64‑encoded) credentials, then resumes pinging.

Technical Points :

Use Python's urllib and urllib2 modules to send HTTP POST requests.

Encode/decode credentials with Base64.

Invoke subprocess to run the system ping command.

Implementation Details : The script can be scheduled as a Windows Task or a Linux crontab entry to start on boot, keeping the network alive with minimal CPU (1‑2%) and memory (≈4 MB) usage.

Code :

<code>#coding=utf8
import urllib, urllib2
import base64
import os, subprocess

username = 'your_base64_encoded_username'
password = 'your_base64_encoded_password'
url = 'https://login.xxxx.edu.cn/auth_action.php'  # campus login URL

def isConnecting():
    # Judge network connect status
    res = subprocess.call('ping 8.8.8.8 -n 1', shell=True)
    return False if res else True

def Signon():
    # Sign on
    data = {
        'action': 'login',
        'username': base64.decodestring(username),
        'password': base64.decodestring(password),
        'ac_id': 1,
        'save_me': 0,
        'ajax': 1,
    }
    data = urllib.urlencode(data)
    response = urllib2.urlopen(url, data)
    response = response.read().decode('utf8')
    print(response)

def main():
    while True:
        if not isConnecting():
            # no‑connect status, attempt sign on
            Signon()

if __name__ == "__main__":
    try:
        main()
    except Exception as e:
        print('[ERROR]:')
        print(e)
</code>

Conclusion : This simple script demonstrates how Python can automate routine network tasks, providing a reliable way to keep a lab computer continuously online for remote work. It can also be deployed on a server, Raspberry Pi, or any always‑on machine.

PythonpingNetwork AutomationBase64subprocessCampus WiFiHTTP POST
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.