A Python SDK for CHU Central Authentication Service
Project description
CHUAuthSDK
CHU统一身份认证 Python SDK
安装
从 PyPI 安装
pip install CHUAuthSDK
默认安装包含验证码图片处理能力(Pillow)与 OCR 能力。
但 ddddocr 当前仅在 Python 3.12 及以下可用;
在 Python 3.13 环境中会自动跳过 ddddocr,图片保存与手动输入验证码流程仍可使用。
本地开发安装
pip install -r requirements.txt
快速开始
from CHUAuthSDK import CHUAuth, CaptchaRequiredError
# 创建认证客户端
auth = CHUAuth(cookie_dir="cookies")
try:
# 登录
session = auth.login("你的学工号", "你的密码")
# 获取用户信息
user_info = auth.get_user_info()
print(f"欢迎, {user_info['cn']}!")
# 使用 session 访问需要认证的资源
resp = session.get("https://course-online.chd.edu.cn/api/radar/rollcalls")
print(resp.json())
except CaptchaRequiredError as e:
# 需要验证码(如果tkinter可用会自动弹出窗口)
print("需要验证码")
if not e.captcha_image:
# tkinter窗口已显示,用户取消了输入
print("用户取消了验证码输入")
else:
# tkinter不可用,保存图片文件
with open("captcha.png", "wb") as f:
f.write(e.captcha_image)
print("请查看 captcha.png 并输入验证码")
# 获取验证码后重新登录
captcha_code = input("请输入验证码: ")
session = auth.login("你的学工号", "你的密码", captcha=captcha_code)
except Exception as e:
print(f"登录失败: {e}")
API 文档
CHUAuth
初始化参数
| 参数 | 类型 | 默认值 | 说明 |
|---|---|---|---|
cas_url |
str |
https://ids.chd.edu.cn |
统一身份认证服务器地址 |
cookie_dir |
Optional[str] |
None |
Cookie 存储目录,None 则不持久化。传入目录路径启用持久化,cookie 文件统一存放在该目录下,自动命名为 "cookies_{username}.json"。例如传入 "cookies" 会生成 "cookies/cookies_2021001.json" |
登录方法
SDK 提供三种登录方式
1. login(username, password, captcha=None, force_relogin=False)
直接传入账号密码登录。
参数:
username: 用户名(学工号/手机号)password: 密码captcha: 验证码(可选)force_relogin: 强制重新登录
返回: requests.Session - 已认证的会话对象
示例:
auth = CHUAuth(cookie_dir="cookies")
session = auth.login("2021001", "password")
2. login_interactive()
CLI 交互式登录,SDK 自动提示用户输入账号密码。
返回: requests.Session - 已认证的会话对象
示例:
auth = CHUAuth(cookie_dir="cookies")
session = auth.login_interactive() # 提示输入账号密码
3. login_batch(accounts_json)
批量登录多个账号。
参数:
accounts_json: JSON 字符串或 JSON 文件路径[ {"username": "2021001", "password": "password1"}, {"username": "2021002", "password": "password2"} ]
返回: Dict[str, Any] - 登录结果字典
{
"2021001": {"success": True, "session": <Session>, "error": None},
"2021002": {"success": False, "session": None, "error": "密码错误"}
}
示例:
auth = CHUAuth(cookie_dir="cookies")
# 传入 JSON 字符串
results = auth.login_batch('[{"username": "2021001", "password": "pwd"}]')
# 或传入 JSON 文件路径
results = auth.login_batch("accounts.json")
其他方法
get_user_info()
获取当前用户信息。
返回: Dict[str, Any] - 用户信息字典
get_cookies_dict()
获取 cookies 字典。
返回: Dict[str, str]
get_session_id()
获取 session ID。
返回: Optional[str]
logout()
登出并清除会话。
异常
AuthError
认证失败异常基类。
CaptchaRequiredError
需要验证码异常。
属性:
captcha_image:bytes- 验证码图片数据
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file chuauthsdk-1.0.0.tar.gz.
File metadata
- Download URL: chuauthsdk-1.0.0.tar.gz
- Upload date:
- Size: 9.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
57ae9e38bf9c5abe7526b9b4e2f9762c78ac528cd235a9cf92903a399101636f
|
|
| MD5 |
0b7a56c104664065e4a2eb49ada64ec0
|
|
| BLAKE2b-256 |
870ddc544c0e66d68d8cbf626ccfbedc23a5bb256a6ac4648c852c967cd95f85
|
File details
Details for the file chuauthsdk-1.0.0-py3-none-any.whl.
File metadata
- Download URL: chuauthsdk-1.0.0-py3-none-any.whl
- Upload date:
- Size: 10.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.13.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7cf8882916a7a60d8ca0241157dd39fc5b97e1492a2a5f245bf415236e9aa05a
|
|
| MD5 |
518bcc860471d23c1def39d196c1f3fd
|
|
| BLAKE2b-256 |
9a1c1e0f8952cf6d33062f33915ba81665d89d854ea596c42bfa88c657728320
|