Commit e81bce63 authored by Wilton Norris's avatar Wilton Norris
Browse files

bug fix

parent 1520abb1
Loading
Loading
Loading
Loading
+20 −23
Original line number Diff line number Diff line
import requests
from bs4 import BeautifulSoup
from time import sleep
import json
import sys
import os
import logging
import os
import re
import sys
from time import sleep

import requests
from bs4 import BeautifulSoup

config = None
logging.basicConfig(format="%(asctime)s - %(levelname)s - %(message)s", level=logging.DEBUG)


def load_config():
    global config
    with open('config.json') as f:
    with open('./config.json') as f:
        config = json.load(f)
    return config

@@ -25,27 +24,25 @@ def main():
    except OSError:
        pass
    logging.info('Reading configurations...')
    global config
    config = load_config()
    times_retry_login = config['max_times_retry_login']
    test_url = config['captive_portal_server'] + '/generate_204'
    test_url = config['captive_portal_server']
    logging.info('Configurations successfully imported.')
    while True:
        logging.info('Checking network status...')
        try:
            login = requests.Session()
            test = login.get(test_url, timeout=30)
        except:
        except requests.RequestException:
            logging.info('Connection FAILED. Try again in ' + str(config['interval_retry_connection']) + ' sec.')
            sleep(config['interval_retry_connection'])
            continue
        while test.status_code != 204:

            soup_login = BeautifulSoup(test.content, 'html5lib')
            if 'CAS' not in soup_login.title.string:
                logging.warning('Not connected to a SUSTC network')
                sleep(config['interval_retry_connection'])
                break
                continue

            logging.info('You are offline. Starting login...')

@@ -73,7 +70,6 @@ def main():
            url = 'https://cas.sustc.edu.cn{}'.format(action)

            h = {

                'Cache-Control': 'max-age=0',
                'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
                'Origin': 'http://cas.sustc.edu.cn',
@@ -81,7 +77,8 @@ def main():
                'Content-Type': 'application/x-www-form-urlencoded',
                'DNT': '1',
                'Referer': url,
                'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4'
                'Accept-Encoding': 'gzip, deflate',
                'Accept-Language': 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4'
            }

            logging.info('Login as ' + config['username'])
@@ -96,23 +93,23 @@ def main():
                logging.error('An error occurred.')
                logging.error(err.h2.text)
                logging.error(err.p.text)
                if times_retry_login > 0:  # If keep trying to login too many times, it may trigger security alarm on the CAS server
                if times_retry_login > 0:
                    # If keep trying to login too many times, it may trigger security alarm on the CAS server
                    logging.info(
                        'Login FAILED. Try again in ' + str(config['interval_retry_login']) + ' sec. ' + str(
                            times_retry_login) + r' attempt(s) remaining.')
                        'Login FAILED. Try again in {time} sec. {attempt} attempt(s) remaining.'
                            .format(time=config['interval_retry_login'], attempt=times_retry_login))
                else:
                    logging.info('Login FAILED.' + r'Attempts used up. The program will quit.')
                    logging.info('Login FAILED. Attempts used up. The program will quit.')
                    sys.exit('Login FAILED')

                sleep(config['interval_retry_login'])

            elif success:
                logging.info('Login successful')
                times_retry_login = config['max_times_retry_login']
                logging.info('Login attempts reset to ' + str(times_retry_login) + ' .')
                logging.info('Login attempts reset to {attempt}.'.format(attempt=times_retry_login))
                break

        logging.info('Online. Re-check status in ' + str(config['interval_check_status']) + ' sec.')
        logging.info('Online. Re-check status in {time} sec.'.format(time=config['interval_check_status']))

        sleep(config['interval_check_status'])

+7 −7
Original line number Diff line number Diff line
@@ -2,13 +2,13 @@
SUSTC network account auto login. Coded in Python. 
Java version with GUI (discontinued): [caslogin-gui](https://github.com/CubicPill/caslogin-gui)
## Requirements
Python 2.7+    
Python3
requests 2.10.0
BeautifulSoup4

## Configuration items

>'captive_portal_server': see [here](https://www.noisyfox.cn/45.html). Default is "http://captive.v2ex.co"
>'captive_portal_server': see [here](https://www.noisyfox.cn/45.html). Default is "http://captive.v2ex.co/generate_204"
'username': Your SUSTC studnet ID
'password': CAS login password
'interval_retry_connection': In second. If the status check failed (e.g. the server was down or there is no Internet connection), how long the program will wait before next attempt. Default value is 30.
+9 −1
Original line number Diff line number Diff line
{"captive_portal_server": "http://captive.v2ex.co", "interval_retry_connection": 30, "max_times_retry_login": 5, "interval_retry_login": 30, "interval_check_status": 60, "password": "YOUR_PASSWD", "username": "YOUR_ID"}
 No newline at end of file
{
  "captive_portal_server": "http://captive.v2ex.co/generate_204",
  "interval_retry_connection": 30,
  "max_times_retry_login": 5,
  "interval_retry_login": 30,
  "interval_check_status": 60,
  "password": "YOUR_PASSWD",
  "username": "YOUR_ID"
}
 No newline at end of file