This repository has been archived by the owner on May 4, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
login.py
55 lines (44 loc) · 1.49 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
43
44
45
46
47
48
49
50
51
52
53
54
55
import os
import re
import pickle
import msession
from ocr import ocr
def login(username: str, password: str):
session = msession.session
res = session.get(msession.urls.cas, verify=False)
lt = re.findall(r'name="lt" value="(.*)"', res.text)
captcha_url = msession.urls.captcha
captcha_path = 'captcha.jpg'
with session.get(captcha_url) as captcha:
with open(captcha_path, mode='wb') as captcha_jpg:
captcha_jpg.write(captcha.content)
#captcha = input('验证码已生成于该目录下,请查看\n并输入验证码')
captcha = ocr(captcha_path)
login_form = {
'username': username,
'password': password,
'captcha': captcha,
'warn': 'true',
'lt': lt[0],
'execution': 'e1s1',
'_eventId': 'submit',
'submit': '登录'
}
post_res = session.post(msession.urls.cas, data=login_form)
if '账号或密码错误' in post_res.text:
print ('账号或密码错误')
return
if '验证码不正确' in post_res.text:
print ('验证码不正确')
return
os.remove('captcha.jpg')
session.get(msession.urls.sso, verify=False)
cookies = session.cookies
if not os.path.exists('cookies'):
os.mkdir('cookies')
if not cookies:
print ('No cookies!')
else:
file_name = 'cookies' + os.sep + username
with open(file_name, mode='wb') as cookies_file:
pickle.dump(session.cookies, cookies_file)