-
Notifications
You must be signed in to change notification settings - Fork 0
/
login.py
executable file
·42 lines (37 loc) · 1.68 KB
/
login.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
import requests
import time
import random
import config # 导入配置文件
# 从配置文件中获取数据
dataLogin = config.dataLogin
dataCheck = config.dataCheck
loginUrl = config.loginUrl
checkStatusUrl = config.checkStatusUrl
header = config.header # 请求头信息
def check_and_login():
# 检查在线状态
response = requests.post(url=checkStatusUrl, headers=header, data=dataCheck)
response.encoding = 'utf-8'
content = response.text.encode().decode("unicode_escape").encode('raw_unicode_escape').decode()
status_index = content.find('"result":"')
# 根据状态执行操作
if content[status_index + 10:status_index + 14] == 'wait' or content[status_index + 10:status_index + 17] == 'success':
print(time.asctime(time.localtime(time.time())), "当前处于在线状态。")
else:
print(time.asctime(time.localtime(time.time())), "当前已经下线,正在尝试登录!")
# 尝试登录
login_response = requests.post(url=loginUrl, headers=header, data=dataLogin)
login_response.encoding = 'utf-8'
login_content = login_response.text.encode().decode("unicode_escape").encode('raw_unicode_escape').decode()
result_index = login_content.find('"result":"')
# 检查登录结果
if login_content[result_index + 10:result_index + 17] == 'success':
print(time.asctime(time.localtime(time.time())), "登录成功!")
while True:
try:
check_and_login()
except Exception as e:
print(time.asctime(time.localtime(time.time())), "监测出错,请检查网络是否连通。", str(e))
time.sleep(1)
continue
time.sleep(random.randint(1, 3))