Commit 60e2df67 authored by Zeyu DONG's avatar Zeyu DONG
Browse files

Merge branch 'master' into dev

parents c3159708 8700fd0e
Loading
Loading
Loading
Loading
+20 −20
Original line number Diff line number Diff line
@@ -17,9 +17,9 @@ from requests.exceptions import ConnectionError
logging.basicConfig(
    format="[%(asctime)s.%(msecs)03d] - %(levelname)s - %(message)s",
    datefmt='%Y/%b/%d %H:%M:%S',
    level=logging.DEBUG,
    filename='CASLogin.log')
logging.getLogger('requests').setLevel(logging.WARNING)
    handlers=[logging.FileHandler(filename='CASLogin.log', mode='a'), logging.StreamHandler()])
logger = logging.getLogger("CASLogin")
logger.setLevel(logging.DEBUG)

login = requests.session()

@@ -31,24 +31,24 @@ def load_config():

def do_login(url, username, password):
    soup_login = BeautifulSoup(login.get(url).content, 'html5lib')
    logging.info('Start to get login information')
    logger.info('Start to get login information')

    info = {}
    for element in soup_login.find('form', id='fm1').find_all('input'):
        if element.has_attr('value'):
            info[element['name']] = element['value']

    logging.info('Login information acquired.')
    logger.info('Login information acquired.')

    info['username'] = username
    info['password'] = password

    url = 'https://cas.sustc.edu.cn/cas/login?service={}'.format(url)

    logging.info('Login as ' + username)
    logger.info('Login as ' + username)

    r = login.post(url, data=info, timeout=30)
    logging.info('Login information posted to the CAS server.')
    logger.info('Login information posted to the CAS server.')

    soup_response = BeautifulSoup(r.content, 'html5lib')
    success = soup_response.find('div', {'class': 'success'})
@@ -67,7 +67,7 @@ def test_network(url):
         raise ResponseError("Invalid status code {code}".format(code=test.status_code))

def main():
    logging.info('Program started.')
    logger.info('Program started.')
    
    try:
        os.chdir(os.path.dirname(sys.argv[0]))  # To read config in the same directory
@@ -76,24 +76,24 @@ def main():
    config = load_config()
    times_retry_login = config['max_times_retry_login']
    test_url = config['captive_portal_server']
    logging.info('Configurations successfully imported.')
    logger.info('Configurations successfully imported.')
    
    while times_retry_login >= 0:
        logging.info('Checking network status...')
        logger.info('Checking network status...')
        try:
            link = test_network(test_url)
            if not link:
                logging.info('You are already logged in.')
                logger.info('You are already logged in.')
                return
            else:
                content = login.get(link, timeout=10).content
                soup_login = BeautifulSoup(content, 'html5lib')
                
                if 'CAS' not in soup_login.title.string:
                    logging.warning('Not connected to a SUSTC network')
                    logger.warning('Not connected to a SUSTC network')
                    return
                
                logging.info('You are offline. Starting login...')
                logger.info('You are offline. Starting login...')
                
                rem_link = re.search(r'window\.location = \'(.*)\';', soup_login.text).group(1)
                hostname = 'http://enet.10000.gd.cn:10001/sz/sz112/'
@@ -102,26 +102,26 @@ def main():
                success, err = do_login(service, config['username'], config['password'])
                
                if err:
                    logging.error('Error occurred: ' + err.text)
                    logger.error('Error occurred: ' + err.text)
                    times_retry_login -= 1
                elif success:
                    logging.info('Login successful')
                    logger.info('Login successful')
                    return
        except ConnectionError as err:
            logging.warn('Connection FAILED. Try again in ' + str(config['interval_retry_connection']) + ' sec.')
            logger.warn('Connection FAILED. Try again in ' + str(config['interval_retry_connection']) + ' sec.')
            times_retry_login -= 1
        
        # If keep trying to login too many times, it may trigger security alarm on the CAS server
        logging.info('Try again in {time} sec. {attempt} attempt(s) remaining.'.format(time=config['interval_retry_connection'], attempt=times_retry_login))
        logger.info('Try again in {time} sec. {attempt} attempt(s) remaining.'.format(time=config['interval_retry_connection'], attempt=times_retry_login))
    
    logging.error('Attempts used up. The program will quit.')
    logger.error('Attempts used up. The program will quit.')


if __name__ == '__main__':
    try:
        main()
    except ResponseError as err:
        logging.error('{msg}, consider updating \'captive_portal_server\''.format(msg=str(err)))
        logger.error('{msg}, consider updating \'captive_portal_server\''.format(msg=str(err)))
    except Exception as e:
        logging.error("".join(traceback.format_exc()))
        logger.error("".join(traceback.format_exc()))
 
 No newline at end of file