feat: 添加多个功能模块和工具脚本
- 新增websocket客户端和服务端实现 - 添加图片压缩工具和快速压缩脚本 - 实现学生信息处理相关API - 添加MQTT客户端和消息处理功能 - 更新.gitignore忽略更多文件类型 - 添加数据库操作工具和示例 - 实现多个测试脚本和工具类
This commit is contained in:
@@ -0,0 +1,78 @@
|
||||
|
||||
|
||||
import base64
|
||||
import os
|
||||
import sys
|
||||
|
||||
import requests
|
||||
|
||||
|
||||
def encrypt_password(password: str, key_word: str) -> str:
|
||||
from Crypto.Cipher import AES
|
||||
|
||||
key = key_word.encode('utf-8')
|
||||
iv = key
|
||||
cypher = AES.new(key, AES.MODE_CFB, iv=iv, segment_size=128)
|
||||
ciphertext = cypher.encrypt(password.encode('utf-8'))
|
||||
return base64.b64encode(ciphertext).decode('utf-8')
|
||||
|
||||
|
||||
def build_basic_auth(client: str) -> str:
|
||||
b64 = base64.b64encode(client.encode('utf-8')).decode('utf-8')
|
||||
return f'Basic {b64}'
|
||||
|
||||
|
||||
def get_token(base_url: str, username: str, password: str) -> dict:
|
||||
enc_key = 'hongxyundapp2025'
|
||||
oauth_client = 'hongxapp:hongxplapp'
|
||||
|
||||
enc_password = encrypt_password(password, enc_key)
|
||||
|
||||
url = f"{base_url.rstrip('/')}/auth/oauth2/token"
|
||||
params = {
|
||||
'username': username,
|
||||
'randomStr': 'clickWord',
|
||||
'code': '',
|
||||
'grant_type': 'password',
|
||||
'scope': 'server',
|
||||
}
|
||||
data = {
|
||||
'password': enc_password,
|
||||
}
|
||||
|
||||
resp = requests.post(
|
||||
url,
|
||||
params=params,
|
||||
data=data,
|
||||
headers={
|
||||
'Authorization': build_basic_auth(oauth_client),
|
||||
'Content-Type': 'application/x-www-form-urlencoded',
|
||||
'Enc-Flag': 'false',
|
||||
},
|
||||
timeout=30,
|
||||
)
|
||||
|
||||
resp.raise_for_status()
|
||||
return resp.json()
|
||||
|
||||
|
||||
def main() -> int:
|
||||
base_url ='http://192.168.0.209:9999'
|
||||
username ='admin'
|
||||
password ='123456'
|
||||
|
||||
try:
|
||||
res = get_token(base_url, username, password)
|
||||
print(res)
|
||||
access_token = res.get('access_token')
|
||||
if access_token:
|
||||
print('\nACCESS_TOKEN=')
|
||||
print(access_token)
|
||||
return 0
|
||||
except Exception as e:
|
||||
print(str(e), file=sys.stderr)
|
||||
return 1
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
raise SystemExit(main())
|
||||
Reference in New Issue
Block a user