-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.py
117 lines (86 loc) · 2.85 KB
/
main.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
import logging
import os
import time
import click
import requests
try:
from config import PASSWORD, USERNAME, VERIFICATION_URL
except ImportError:
PASSWORD = USERNAME = None
VERIFICATION_URL = 'https://drcom.szu.edu.cn/'
TEST_URL = 'https://www.baidu.com/'
def get_logger():
logger = logging.getLogger('fuck_drcom')
logger.setLevel(logging.INFO)
formatter = logging.Formatter('%(asctime)s: %(message)s')
ch = logging.StreamHandler()
ch.setFormatter(formatter)
logger.addHandler(ch)
return logger
logger = get_logger()
def check_connection():
try:
requests.head(TEST_URL, timeout=2)
except requests.RequestException:
return False
return True
def login(username, password):
form_data = {
'DDDDD': username,
'upass': password,
'R1': 0,
'R2': '',
'R6': 0,
'para': 00,
'0MKKey': 123456
}
try:
res = requests.post(VERIFICATION_URL, form_data)
except requests.RequestException:
return False
return res.status_code == 200 and check_connection()
def logout():
res = requests.get(os.path.join(VERIFICATION_URL, 'F.html'))
return res.status_code == 200 and not check_connection()
class OptionPromptNull(click.Option):
# https://stackoverflow.com/questions/45868549/
_value_key = '_default_val'
def get_default(self, ctx):
if not hasattr(self, self._value_key):
default = super(OptionPromptNull, self).get_default(ctx)
setattr(self, self._value_key, default)
return getattr(self, self._value_key)
def prompt_for_value(self, ctx):
default = self.get_default(ctx)
# only prompt if the default value is None
if default is None:
return super(OptionPromptNull, self).prompt_for_value(ctx)
return default
PRINT_EACH = 60 * 12
@click.command()
@click.option('-u', '--username', prompt=True, default=USERNAME, cls=OptionPromptNull)
@click.option('-p', '--password', prompt=True, hide_input=True, default=PASSWORD, cls=OptionPromptNull)
@click.option('-a', '--keep_alive', is_flag=True, cls=OptionPromptNull)
def cli(username, password, keep_alive):
print_cnt = PRINT_EACH
if keep_alive:
logger.info('keep alive mode on')
while True:
if not check_connection():
logger.info('network disconnect, retry login')
if login(USERNAME, PASSWORD):
logger.info('successfully login as {}'.format(USERNAME))
else:
logger.info('login fail, retry later')
else:
logger.debug('conection ok')
if print_cnt == PRINT_EACH:
logger.info('process alive')
print_cnt = 0
else:
print_cnt += 1
if not keep_alive:
return
time.sleep(60)
if __name__ == "__main__":
cli()