Commit fbe060b3 authored by Zeyu DONG's avatar Zeyu DONG
Browse files

new tree

parent 23f0757a
Loading
Loading
Loading
Loading
+42 −45
Original line number Diff line number Diff line
@@ -27,73 +27,75 @@ def do_login(url, username, password):
   soup_login = BeautifulSoup(login.get(url).content, 'html5lib')
   logging.info('Start to get login information')

   action = soup_login.find('form', id='fm1')['action']  # get login url
   logging.debug('action= ' + action)

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

   logging.debug(info)

   logging.info('Login information acquired.')

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

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

   logging.info('Login as ' + username)

   r = login.post(url, data=info, timeout=30)
   logging.info('Login information posted to the CAS server.')
   
   soup_response = BeautifulSoup(r.content, 'html5lib')
   success = soup_response.find('div', {'class': 'success'})
   err = soup_response.find('div', {'class': 'errors', 'id': 'msg'})
   
   return success, err


def test_network(url):
   try:
      test = login.get(url, timeout=30)
      content = test.content
      if test.status_code == 204:
      with login.get(url, timeout=10, allow_redirects=False) as test:
          if test.status_code == 200:
             status = CONNECTED
          elif test.status_code == 302:
             content = login.get(test.headers['Location'], timeout=10).content
             soup_login = BeautifulSoup(content, 'html5lib')
             if 'CAS' not in soup_login.title.string:
                logging.warning('Not connected to a SUSTC network')
                status = CONNECTED
             else:
                status = NEED_LOGIN
   except requests.Timeout:
          else:
             logging.warning('Recieved status code {code}, consider updating \'captive_portal_server\''.format(code=test.status_code))
             status = CONNECTION_TIMEOUT
   except requests.RequestException:
       status = CONNECTION_TIMEOUT
      content = None
   
   return status, content
   return status


def main():
   logging.info('Program started.')
   
   try:
      os.chdir(os.path.dirname(sys.argv[0]))  # To read config in the same directory
   except OSError:
      pass
   
   config = load_config()
   times_retry_login = config['max_times_retry_login']
   test_url = config['captive_portal_server']
   logging.info('Configurations successfully imported.')
   
   while True:
      logging.info('Checking network status...')
      status, content = test_network(test_url)
      status = test_network(test_url)
      if status == CONNECTION_TIMEOUT:
         logging.info('Connection FAILED. Try again in ' + str(config['interval_retry_connection']) + ' sec.')
         sleep(config['interval_retry_connection'])
         continue

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

         times_retry_login -= 1
      elif status == CONNECTED:
         logging.info('You are already logged in.')
         break
      elif status == NEED_LOGIN:
         logging.info('You are offline. Starting login...')

         hostname = 'http://enet.10000.gd.cn:10001/sz/sz112/'
@@ -105,25 +107,20 @@ def main():
         if err:
            times_retry_login -= 1
            logging.error('Error occurred: ' + err.text)
            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 {time} sec. {attempt} attempt(s) remaining.'
                     .format(time=config['interval_retry_login'], attempt=times_retry_login))
            else:
               logging.info('Login FAILED. Attempts used up. The program will quit.')
               sys.exit(1)
            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 {attempt}.'.format(attempt=times_retry_login))
            break
      
      logging.info('Online. Re-check status in {time} sec.'.format(time=config['interval_check_status']))
      if times_retry_login > 0:
         # 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))
      else:
         logging.info('Attempts used up. The program will quit.')
         sys.exit(-1)
      sleep(config['interval_retry_connection'])
   
      sleep(config['interval_check_status'])
   logging.info('Online.')
   sys.exit(0)


if __name__ == '__main__':