-
Notifications
You must be signed in to change notification settings - Fork 1
/
NCUWLAN.py
132 lines (106 loc) · 3.66 KB
/
NCUWLAN.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# -*- coding: utf-8 -*-
# Author: NoCLin
import configparser
import requests
import re
import logging
logger = logging.getLogger('root')
def get_middle_str(content, left, right):
pat = re.compile(left + '(.*?)' + right, re.S)
result = pat.findall(content)
return result
def print_obj(obj):
print('\n'.join(['%s:%s' % item for item in obj.__dict__.items()]))
def http_head(url):
try:
r = requests.head(url, timeout=1)
if r.status_code == 200:
return True
logger.error("http_head " + url + " status:" + str(r.status_code))
except Exception as e:
logging.exception(e)
return False
def is_online_by_baidu():
return http_head("https://www.baidu.com/")
class NCUWLAN():
URL_DETECT = "http://aaa.ncu.edu.cn:802/srun_portal_pc.php"
URL_AUTH = "http://aaa.ncu.edu.cn:803/include/auth_action.php"
URL_STATUS = "http://aaa.ncu.edu.cn:803/srun_portal_pc_succeed.php"
USERNAME = ""
PASSWORD = ""
HEADERS = {
}
def __init__(self, username=None, password=None):
if username is None:
ini_parser = configparser.ConfigParser()
with open("./account.ini") as fp:
ini_parser.read_file(fp)
self.USERNAME = ini_parser.get("NCUWLAN", "username")
self.PASSWORD = ini_parser.get("NCUWLAN", "password")
else:
self.USERNAME = username
self.PASSWORD = password
logger.debug("USERNAME:" + self.USERNAME)
def status(self):
try:
r = requests.get(self.URL_STATUS)
r_text = r.content.decode("UTF-8")
except Exception as e:
logging.exception(e)
return False
logger.debug("Status:" + r_text)
keys = ['username', 'ip', 'data_usage', 'time_usage', 'money']
keys_cn = ['用户名', 'IP地址', '已用流量', '已用时长', '帐户余额']
values = get_middle_str(r_text, 'style="font-size:18px;color:#fd7100;">', "</span>")
rt = {}
for index, v in enumerate(keys):
rt[keys_cn[index]] = values[index]
return rt
def login(self):
data = {
"action": "login",
"username": self.USERNAME,
"password": self.PASSWORD,
"ac_id": "1",
"user_ip": "",
"nas_ip": "",
"user_mac": "",
"save_me": "1",
"ajax": "1"
}
try:
r = requests.post(self.URL_AUTH, data=data, headers=self.HEADERS)
r_text = r.content.decode("UTF-8")
except Exception as e:
logging.exception(e)
return False
logger.debug("Login:" + r_text)
if r.text.find("login_ok") > -1:
return True
else:
# TODO: 解析 登录间隔10秒等已知情况
logger.error("登录失败,未知返回值:" + r_text)
return False
def logout(self):
data = {
"action": "logout",
"username": "",
"password": "",
"ajax": "1"
}
try:
r = requests.post(self.URL_AUTH, data=data, headers=self.HEADERS)
r_text = r.content.decode("UTF-8")
except Exception as e:
logging.exception(e)
return False
logger.debug("Logout:" + r_text)
if r_text.find("您似乎未曾连接到网络...") > -1:
return True
if r_text.find("网络已断开") > -1:
return True
else:
logger.error("登出失败,未知返回值:" + r_text)
return False
def is_online_by_ncu(self):
return http_head(self.URL_DETECT)