南京大学校园网Portal认证Python脚本

查看了校园网认证的网页源码,搞清楚认证过程。使用python写个认证的脚本,主要还是自己用起来比较方便,老是拿网页还是挺烦的。

一、脚本在Python3环境下运行

1.登录命令 

python njunet.py -do l -user ab1234567 -passwd 0123456

2.登出命令

python njunet.py -do logout

3.查看在线信息

python njunet.py -do i

二、源码

#-*-encoding:utf-8-*-
import argparse
import urllib.request, urllib.parse, urllib.error
import time, ast

username = ''
password = ''

portal_url = 'http://p.nju.edu.cn'
headers = {
    'Accept': 'application/json, text/javascript, */*; q=0.01',
    'Accept-Encoding': 'gzip, deflate',
    'Accept-Language': 'zh-CN,zh;q=0.9',
    'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/69.0.3497.100 Safari/537.36'
}
language = 'cn'
io_url = '/portal_io/'
auth_type = 'pap'
pageNum = 5
userinfo = None
selfservice = None
service = {
        'current' : None,
        'next' : None,
        'chose' : []
    }
refresh = True,
server_time = None

#https://www.cnblogs.com/MnCu8261/p/5880764.html
def long2ip(long):
    floor_list=[]
    yushu=long
    for i in reversed(range(4)):
        res=divmod(yushu,256**i)
        floor_list.append(str(res[0]))
        yushu=res[1]
    return '.'.join(floor_list)

def letterorcharacter(ch):
    if ord(ch) <= 127 and ord(ch) >= 0:
        return 1
    else :
        return 0
       

def myFormatPrint(string, length=0, align='l', symbol=' '):
    i = 0
    temp = ''
    str_len = len(string)
    while i < length:
        temp += symbol
        i += 1
    i = 0
    str_sp = 0
    while i < str_len:
        if letterorcharacter(string[i]):
            str_sp += 1
        else:
            str_sp += 2
        i += 1
    if align == 'l':
        return string + temp[str_sp:]
    if align == 'r':
        return temp[:-str_sp] + string
    if align == 'c':
        l_c = (length - str_sp)//2
        r_c = length - l_c - str_sp
        return temp[:-(str_sp+r_c)] + string + temp[l_c+str_sp:]

def loginRequest(user, passwd):
    params = {
        'username': username if user == '' else user,
        'password': password if passwd == '' else passwd
    }
    if params['username'] == '' or params['password'] == '':
        return
    params_encode = urllib.parse.urlencode(params).encode('utf8')
    request_url = portal_url + io_url + 'login'
    login_request = urllib.request.Request(url = request_url, data = params_encode, headers = headers)
    try:
        response_data = urllib.request.urlopen(login_request)
        response = response_data.read().decode('utf8')
        return response
    except urllib.error.HTTPError as e:
        print("HTTPError")
        print(e.code)
    except urllib.error.URLError as e:
        print("URLError")
        print(e.reason)
    
def getInformation():
    request_url = portal_url + io_url + 'selfservice/volume/getlist'
    request = urllib.request.Request(url = request_url, headers = headers)
    try:
        response_data = urllib.request.urlopen(request)
        response = response_data.read().decode('utf8')
        return response
    except urllib.error.HTTPError as e:
        print("HTTPError")
        print(e.code)
    except urllib.error.URLError as e:
        print("URLError")
        print(e.reason)


def getOnline():
    request_url = portal_url + io_url + 'selfservice/online/getlist'
    params = {
        'page': 1,
        'rows': 5
    }
    params_encode = urllib.parse.urlencode(params).encode('utf8')
    request = urllib.request.Request(url = request_url, data = params_encode, headers = headers)
    try:
        response_data = urllib.request.urlopen(request)
        response = response_data.read().decode('utf8')
        return response
    except urllib.error.HTTPError as e:
        print("HTTPError")
        print(e.code)
    except urllib.error.URLError as e:
        print("URLError")
        print(e.reason)

def logoutRequest():
    request_url = portal_url + io_url + 'logout'
    request = urllib.request.Request(url = request_url, headers = headers)
    try:
        response_data = urllib.request.urlopen(request)
        response = response_data.read().decode('utf8')
        return response
    except urllib.error.HTTPError as e:
        print("HTTPError")
        print(e.code)
    except urllib.error.URLError as e:
        print("URLError")
        print(e.reason)


def convertTime(time, type):
	h = 0
	m = 0
	s = 0
	if type == 'S':
		h = time // 3600
		m = (time - h * 3600) // 60
		return str(m) + '分钟' if h == 0 else str(h) + '小时' + str(m) + '分钟'
	elif type == 'D':
		h = time // 3600
		m = (time - h * 3600) // 60
		s = time - h * 3600 - m * 60
		return (("" if h > 9 else "0") + str(h)) + ":" + (("" if m > 9 else "0") + str(m)) + ":" + (("" if s > 9 else "0") + str(s))

def printLoginInformation(response):
    #查询在线信息
    online_information = ast.literal_eval(getInformation())
    print(myFormatPrint('', 35, align='l', symbol = '*'))
    print(myFormatPrint('用户名:', 15, align='r', symbol = ' ') + response['userinfo']['username'])
    print(myFormatPrint('登录信息:', 15, align='r', symbol = ' ') + response['reply_msg'])
    print(myFormatPrint('全名:', 15, align='r', symbol = ' ') + response['userinfo']['fullname'])
    print(myFormatPrint('账户余额:', 15, align='r', symbol = ' ') + str(int(response['userinfo']['balance'])//100)+'元'+str(int(response['userinfo']['balance'])%100)+'分')
    print(myFormatPrint('累计时长:', 15, align='r', symbol = ' ') + convertTime(online_information['rows'][0]['total_ipv4_volume'], 'S'))
    print(myFormatPrint('当前套餐:', 15, align='r', symbol = ' ') + response['userinfo']['service_name'])
    print(myFormatPrint('所在区域:', 15, align='r', symbol = ' ') + response['userinfo']['area_name'])
    print(myFormatPrint('', 35, align='l', symbol = '*'))

def printState(online_information, total_time):
    print(myFormatPrint('', 35, align='l', symbol = '*'))
    print(myFormatPrint('用户名:', 15, align='r', symbol = ' ') + online_information['rows'][0]['username'])
    print(myFormatPrint('登录信息:', 15, align='r', symbol = ' ') + online_information['rows'][0]['area_type_name'])
    print(myFormatPrint('全名:', 15, align='r', symbol = ' ') + online_information['rows'][0]['fullname'])
    print(myFormatPrint('累计时长:', 15, align='r', symbol = ' ') + convertTime(total_time['rows'][0]['total_ipv4_volume'], 'S'))
    print(myFormatPrint('IP地址:', 15, align='r', symbol = ' ') + long2ip(online_information['rows'][0]['user_ipv4']))
    print(myFormatPrint('MAC地址:', 15, align='r', symbol = ' ') + online_information['rows'][0]['mac'])
    print(myFormatPrint('当前套餐:', 15, align='r', symbol = ' ') + online_information['rows'][0]['service_name'])
    print(myFormatPrint('所在区域:', 15, align='r', symbol = ' ') + online_information['rows'][0]['area_name'])
    print(myFormatPrint('', 35, align='l', symbol = '*'))

if __name__ == '__main__':
    ## hyperparameters
    parser = argparse.ArgumentParser(description='南京大学校园网认证')
    parser.add_argument('-do', type=str, default='l', help='用来指定登录login,登出logout和查询')
    parser.add_argument('-user', type=str, default='', help='用户名')
    parser.add_argument('-passwd', type=str, default='', help='密码')
    args = parser.parse_args()
    
    if args.do in ['login','Login', 'l']:
        response = loginRequest(args.user, args.passwd)
        if response is None:
            print('请检查网络连接或检查用户名和密码')
        else:
            response = ast.literal_eval(response)
            if response['reply_code'] != 1:
                print('请检查网络连接或检查用户名和密码')
                print('服务器信息:', response['reply_msg'])
            else:
                time.sleep(2)
                printLoginInformation(response)
    if args.do in ['logout','Logout', 'exit','o']:
        response = logoutRequest()
        if response is None:
            pass
        else:
            response = ast.literal_eval(response)
            if response['reply_code'] != 101:
                print('请检查网络连接或服务器信息')
                print('服务器信息:', response['reply_msg'])
            else:
                print(myFormatPrint('', 35, align='l', symbol = '*'))
                print(myFormatPrint('服务器返回码:', 15, align='r', symbol = ' ')+str(response['reply_code']))
                print(myFormatPrint('当前状态:', 15, align='r', symbol = ' ')+response['reply_msg'])
                print(myFormatPrint('', 35, align='l', symbol = '*'))
    if args.do in ['i','I']:
        total_time_resp = getInformation()
        online_info_resp = getOnline()
        if total_time_resp is None or online_info_resp is None:
            print('请检查网络连接')
        else:
            total_time = ast.literal_eval(total_time_resp)
            online_information = ast.literal_eval(online_info_resp)
            if total_time['reply_code'] == 0 and online_information['reply_code'] == 0:
                printState(online_information, total_time)
            else :
                print('未登录校园网')
                print('服务器信息:', online_information['reply_msg'])

 

  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值