82 lines
3.3 KiB
Python
82 lines
3.3 KiB
Python
import json
|
||
import requests
|
||
from urllib.parse import quote
|
||
import base64
|
||
from bs4 import BeautifulSoup
|
||
import random
|
||
from hashlib import md5
|
||
import datetime
|
||
|
||
# 链接教务获取信息
|
||
def connection(username,password):
|
||
try:
|
||
s = requests.Session()
|
||
# 获取统一身份系统的网页
|
||
r = s.get(url='http://mysso-cust-edu-cn-s.webvpn.cust.edu.cn:8118/cas/login?service=https%3A%2F%2Fwebvpn.cust.edu.cn%2Fauth%2Fcas_validate%3Fentry_id%3D1')
|
||
# soup = BeautifulSoup(r.text,'lxml')
|
||
soup = BeautifulSoup(r.text,'html.parser')
|
||
execution=soup.find_all(name='input')[6]['value']
|
||
formdata={
|
||
'username':username,
|
||
'password':password,
|
||
'execution':execution,
|
||
'_eventId':'submit',
|
||
'geolocation':''
|
||
}
|
||
r = s.post(url='http://mysso-cust-edu-cn-s.webvpn.cust.edu.cn:8118/cas/login?service=https%3A%2F%2Fwebvpn.cust.edu.cn%2Fauth%2Fcas_validate%3Fentry_id%3D1',data=formdata)
|
||
soup=BeautifulSoup(r.text,'html.parser')
|
||
flag = soup.find(name='title')
|
||
if(flag.text=="手机号设置"):
|
||
return ('没用手机号登陆过教务', 450)
|
||
r = s.get(url='http://portal-cust-edu-cn-s.webvpn.cust.edu.cn:8118/custp/index')
|
||
soup=BeautifulSoup(r.text,'html.parser')
|
||
try:
|
||
ip = soup.findAll(name='a')[7]['href'][7:].split("-")
|
||
except:
|
||
return ('账号或密码错误', 422)
|
||
r = s.get(url='http://mysso-cust-edu-cn-s.webvpn.cust.edu.cn:8118/cas/login?service=http://'+ip[0]+'.'+ip[1]+'.'+ip[2]+'.'+ip[3]+':8080/welcome',allow_redirects=False)
|
||
ticket = r.headers['Location'][68:]
|
||
asp_net_sessionid_param = {'Ticket':ticket,'Url':'http://'+ip[0]+'.'+ip[1]+'.'+ip[2]+'.'+ip[3]+':8080/welcome'}
|
||
asp_net_sessionid_param = base64.b64encode(quote(json.dumps(asp_net_sessionid_param)).encode('utf-8')).decode('utf-8')
|
||
asp_net_sessionid_param = {'param':asp_net_sessionid_param}
|
||
headers = {'Content-Type': 'application/json'}
|
||
r = s.post(url='http://'+ip[0]+'-'+ip[1]+'-'+ip[2]+'-'+ip[3]+'-8080-p.webvpn.cust.edu.cn:8118/api/LoginApi/LGSSOLocalLogin?sf_request_type=ajax',data=json.dumps(asp_net_sessionid_param),headers=headers)
|
||
data = json.loads(r.content.decode('utf-8'))
|
||
student_name = data['data']['StudentDto']['XM']
|
||
student_id = data['data']['StudentDto']['XH']
|
||
return ({'name': student_name, 'id': student_id}, 512)
|
||
except Exception as e:
|
||
print(e)
|
||
return ('教务挂了', 511)
|
||
|
||
# 给str签名,用于加密密码
|
||
def signCode(code):
|
||
d = str(code)
|
||
d = d.replace(' ', '')
|
||
md = md5()
|
||
md.update(d.encode('utf-8'))
|
||
r = md.hexdigest().upper()
|
||
return r
|
||
|
||
# 生成唯一不重名字符串
|
||
def randomId():
|
||
nowTime = datetime.datetime.now().strftime("%Y%m%d%H%M%S") # 生成当前时间
|
||
randomNum = random.randint(0, 100) # 生成的随机整数n,其中0<=n<=100
|
||
if randomNum <= 10:
|
||
randomNum = str(0) + str(randomNum)
|
||
uniqueNum = str(nowTime) + str(randomNum)
|
||
return uniqueNum
|
||
|
||
# MD5 校验
|
||
def md5Check(data):
|
||
d = data.copy()
|
||
try:
|
||
d.pop('sign')
|
||
except KeyError:
|
||
pass
|
||
d = str(d)
|
||
d = d.replace(' ', '')
|
||
md = md5()
|
||
md.update(d.encode('utf-8'))
|
||
r = md.hexdigest().upper()
|
||
return r == data['sign'] |