-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNJUNet.py
129 lines (99 loc) · 2.9 KB
/
NJUNet.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
"""
Python script for login and logout the NJU network account
"""
from urllib import parse, request
import argparse
import json
__all__ = [
"login",
"logout",
"ShowResponse",
]
_URLs = {
"login": "http://p.nju.edu.cn/portal_io/login",
"logout": "http://p.nju.edu.cn/portal_io/logout",
}
def login(username, password):
"""
Logging in to the NJU network account
Parameters
----------
username : str
password : str
Returns
-------
response : dict
The response from the server
"""
if not isinstance(username, str) or not isinstance(password, str):
raise ValueError(
"The `username` and `password` parameter must be string"
)
params = parse.urlencode({
'username': username,
"password": password,
}).encode("ascii")
with request.urlopen(_URLs["login"], data=params) as fp:
response = json.loads(fp.read().decode("utf-8"))
return response
def logout():
"""
Logging out from the NJU network account
Returns
-------
response : dict
The response from the server
"""
with request.urlopen(_URLs["logout"]) as fp:
response = json.loads(fp.read().decode("utf-8"))
return response
def ShowResponse(response, *, verbose=False):
"""
Show the response from the server
Parameters
----------
response : dict
The content of the response
verbose : boolean, optional
Whether to display the full content of the response
default: False
"""
if verbose:
for key, value in response.items():
if isinstance(value, dict):
print(f"{key}: " + "{")
for sub_key, sub_value in value.items():
print(f" {sub_key}: {sub_value}")
print("}")
else:
print(f"{key}: {value}")
else:
print(f"reply_msg: {response['reply_msg']}")
def ArgParser():
"""
Parsing the command line arguments
"""
parser = argparse.ArgumentParser(
description="Login or logout the NJU network account!"
)
parser.add_argument(
"action", type=str, choices=("login", "logout"),
help="Login or logout the NJU network account?"
)
parser.add_argument("-u", "--username", type=str, help="The username.")
parser.add_argument(
"-v", "--verbose", action="store_true",
help="Show the full content of the response from the server."
)
args = parser.parse_args()
return args.action, args.username, args.verbose
if __name__ == "__main__":
from getpass import getpass
action, username, verbose = ArgParser()
if action == "login":
if username is None:
username = input("Username:")
response = login(username=username, password=getpass())
else:
response = logout()
ShowResponse(response, verbose=verbose)