#!/usr/bin/python3 import urllib.parse import urllib.request import urllib.response import logging import re import argparse class login_manager: logger = logging.getLogger('login_manager') login_url = 'http://10.1.206.13/a70.htm' logout_url = 'http://10.1.206.13/F.htm' def __init__(self, username: str = '', password: str = ''): self.username = username self.password = password @staticmethod def generate_post_data(username: str, password: str): data_template = { 'DDDDD': None, 'upass': None, 'R1': 0, 'R2': '', 'R3': 0, 'R6': 0, 'para': 00, '0MKKey': 123456, 'buttonClicked': '', 'redirect_url': '', 'err_flag': '', 'username': '', 'password': '', 'user': '', 'cmd': '', 'Login': '' } data_template['DDDDD'] = username data_template['upass'] = password login_manager.generate_post_data_hook(data_template) login_manager.logger.debug('the generated post data is %s', str(data_template)) return bytes(urllib.parse.urlencode(data_template), 'utf-8') @staticmethod def generate_post_data_hook(post_data: dict) -> None: username = post_data['DDDDD'] username += '@bistu' post_data['DDDDD'] = username def login(self): headers = { 'Content-Type': 'application/x-www-form-urlencoded', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8', 'Accept-Language': 'en-US,zh-CN;q=0.5', 'Accept-Encoding': 'gzip, deflate' } post_data = self.generate_post_data(self.username, self.password) self.logger.debug('the dest url is: %s', self.login_url) self.logger.debug('the headers is: %s', str(headers)) self.logger.debug('the encoded post data is %s', str(post_data)) req = urllib.request.Request(url=self.login_url, data=post_data, headers=headers, method='POST') self.logger.info('sending the login request...') with urllib.request.urlopen(req) as response: response_body = response.read().decode('gbk') html_title = re.search('(.*)', response_body)[1] self.logger.info('http_status: %d', response.status) self.logger.info('title_of_response_html: %s', html_title) self.logger.info('login_state: %s', 'success' if html_title == '认证成功页' else 'fail') @staticmethod def logout(): req = urllib.request.Request(url=login_manager.logout_url) login_manager.logger.info('sending the logout request...') with urllib.request.urlopen(req) as response: response_body = response.read().decode('gbk') html_title = re.search('(.*)', response_body)[1] login_manager.logger.info('http_status: %d', response.status) login_manager.logger.info('title_of_response_html: %s', html_title) login_manager.logger.info('logout_state: %s', 'success' if html_title == '信息页' else 'fail') def load_auth_info(path: str): with open(path, 'r', encoding='utf-8') as file: username = file.readline().strip() password = file.readline().strip() return username, password def main(): logging.basicConfig(level=logging.INFO) logger = logging.getLogger('main') parser = argparse.ArgumentParser() parser.description = 'login or logout bistu wifi' parser.epilog = 'the config file format(without quote): "{username}\\n{password}". The "\\n" is a newline charactor' parser.add_argument('username', nargs='?', type=str, help='username you use to login bistu wifi, logout if not provided') parser.add_argument('password', nargs='?', type=str, help='password you use to login bistu wifi, logout if not provided') parser.add_argument('-o', '--logout', nargs='?', type=bool, help='logout bistu wifi') parser.add_argument('-c', '--config', nargs='?', type=str, help='load username and password from config file') args = parser.parse_args() logger.debug('parse argument: username= %s', args.username) logger.debug('parse argument: password= %s', args.password) logger.debug('parse argument: logout= %s', args.logout) if args.config is not None: username, password = load_auth_info(args.config) manager = login_manager(username, password) manager.login() elif args.username is not None and args.password is not None: manager = login_manager(args.username, args.password) manager.login() elif args.logout: login_manager.logout() else: parser.print_help() if __name__ == '__main__': main()