-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathweb.py
More file actions
71 lines (63 loc) · 2.6 KB
/
web.py
File metadata and controls
71 lines (63 loc) · 2.6 KB
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
from fastapi import FastAPI, Request
from fastapi.responses import HTMLResponse
from fastapi.responses import JSONResponse
from fastapi.templating import Jinja2Templates
import requests
import hashlib
import json
import time
import uvicorn
def login_api(studentId, password, captcha,
api_url="http://szgxjx.csu.edu.cn/api/security-server/dietc/sys/authorizing/login"):
## 密码转化为md5
password_md5 = hashlib.md5(password.encode()).hexdigest()
payload = {
"queryParam": "QHP2xQRpUEvs1766113234391",
"password": password_md5,
"username": studentId,
"code": captcha
}
headers = {
"User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/143.0.0.0 Safari/537.36",
"Connection": "keep-alive",
"Accept": "application/json, text/plain, */*",
"Accept-Encoding": "gzip, deflate",
"Content-Type": "application/json",
"DNT": "1",
"Origin": "http://szgxjx.csu.edu.cn",
"Referer": "http://szgxjx.csu.edu.cn/zndxstudent/",
"Accept-Language": "zh-CN,zh;q=0.9"
}
response = requests.post(api_url, data=json.dumps(payload), headers=headers)
response.raise_for_status()
return response.json()
app = FastAPI()
templates = Jinja2Templates(directory="templates")
@app.get("/", response_class=HTMLResponse)
async def main(request: Request):
current_timestamp = str(int(time.time() * 1000))
captcha_url = "http://szgxjx.csu.edu.cn/api/security-server/dietc/servlet/patchcaServlet?action=QHP2xQ&appid=RpUEvs&key=1766113234391"
return templates.TemplateResponse(
request=request, name="main.html", context={"welcome": "CSU工训报告自动化系统", "captcha_url": captcha_url}
)
@app.post("/login", response_class=JSONResponse)
async def login(request: Request):
## 解析body中的json数据
data = await request.json()
studentId = data.get("username")
password = data.get("password")
captcha = data.get("captcha")
result = login_api(studentId, password, captcha)
response = {
"success": False,
"message": ""
}
if result['status'] != 200:
response['message'] = result.get('message', '登录失败,未知错误')
else:
response['success'] = True
response['message'] = "登录成功"
response['name'] = result['datas']['user']['realName']
response['token'] = result['datas']['token']
return JSONResponse(content=response)
uvicorn.run(app, host="127.0.0.1", port=8000)