Files
python----/新平台/pctoken.py
T
admin 2d8c1ea8f9 chore: 批量新增各类工具脚本与配置文件
1. 新增音频录制、下载、上传相关脚本
2. 新增数据库操作、API调用工具
3. 新增Excel数据处理脚本
4. 新增弱密码检测脚本
2026-06-14 17:47:15 +08:00

79 lines
1.6 KiB
Python

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)
print(enc_password)
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())