initial commit, finish the logic of login and logout

This commit is contained in:
leafee98 2021-10-11 12:12:44 +08:00
commit 6bb5f77141

126
login_manager.py Normal file
View file

@ -0,0 +1,126 @@
#!/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('<title>(.*)</title>', 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('<title>(.*)</title>', 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 main():
logging.basicConfig(level=logging.INFO)
logger = logging.getLogger('main')
parser = argparse.ArgumentParser(description='login or logout bistu wifi')
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')
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.username is None or args.password is None or args.logout:
login_manager.logout()
else:
manager = login_manager(args.username, args.password)
manager.login()
if __name__ == '__main__':
main()