南京大学统一认证登陆
从账号密码拿到 CASTGC cookie
import nju_login
response = nju_login.login('学号', '统一认证密码')
print(response.cookies["CASTGC"])
安装
pip install nju-login-simple
登陆南大网站
正常使用浏览器时,南大SSO的登陆流程一般是:
- 你去访问目标网站,比如课表 https://ehall.nju.edu.cn/appShow?appId=4770397878132218
- 目标网站发现你没登陆,于是给你重定向到统一认证
- 你在统一认证完成登陆,它给你重定向回目标网站,此时URL会带上一个ticket
- 目标网站根据ticket给你设置cookie,然后重定向到你一开始想去的页面
这里面只有第三步需要人工参与,其它地方都是自动重定向。
本项目要做的就是帮你跳过第三步。如果你访问统一认证网站时带上了有效的CASTGC cookie,那么它就会知道你已经登陆,并直接给你ticket然后带你重定向。
因此,如果想借助本项目登陆南大网站(比如ehall或者epay啥的),一般流程是:
- 调用login,拿到castgc
- 将castgc加到自己的request session的cookies里。此时你的session在统一认证有登陆,在服务站没登陆。
- 用自己的session去访问nju服务。经过几次自动跳转,你就在服务站登陆上了。
示例登陆代码
感谢 @Chen-Rong-Zi 提供的示例代码和文档完善建议。
import nju_login
import requests
# 登录获取 CASTGC
resp = nju_login.login('学号', '密码')
castgc = resp.cookies.get('CASTGC')
# 构建 session 并触发 SSO
session = requests.Session()
session.cookies.set('CASTGC', castgc, domain='authserver.nju.edu.cn')
session.get('https://epay.nju.edu.cn/epay/h5/nju/electric/index')
# 现在可以访问具体页面
r = session.get('https://epay.nju.edu.cn/epay/h5/nju/electric/charge?id=53463')
print(r.text)
错误处理
本项目自定义的错误都在[src/nju_login/nju_login.py]中定义了:
class NJULoginError(BaseException):
"""登陆流程中的所有错误。错误基类,不直接抛出。"""
pass
class CaptchaFailError(NJULoginError):
"""过验证码失败"""
pass
class LoginError(NJULoginError):
"""提交用户名密码登录时失败"""
pass
此外,也可能抛出数组越界、网络失败等其他错误。
Metadata
Release files for nju-login-simple 1.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| nju_login_simple-1.2.tar.gz | 6.4 kB | With provenance | Details for nju_login_simple-1.2.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| nju_login_simple-1.2-py3-none-any.whl | Python 3 | none | any | Details for nju_login_simple-1.2-py3-none-any.whl |
Total release size: 13.4 kB
Release files / nju_login_simple-1.2.tar.gz
| Download URL | nju_login_simple-1.2.tar.gz |
|---|---|
| Size | 6.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a41aabe35e032930bf1dc437c433b796c6812767b23b99c449f92babfbdfcfca
|
|
BLAKE2b-256 checksum How to use checksums |
2ef2058d88ce403801c406bc985b9e1883f2cf5771a9454ddd46687d295b586c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.
Transparency logRelease files / nju_login_simple-1.2-py3-none-any.whl
| Download URL | nju_login_simple-1.2-py3-none-any.whl |
|---|---|
| Size | 7.0 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
f7a48761f612253407f73d11c625a525538dca0e4521cd98564dd777e133448a
|
|
BLAKE2b-256 checksum How to use checksums |
f2d38ffc535d968e03c36bda896f4359fbd6b5600e414a10aec9351bf6befcf0
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.
Transparency logSecurity for nju-login-simple 1.2
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for nju_login_simple-1.2.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Aug 3, 2026.
Transparency logA note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.