Skip to main content

南京大学统一认证登陆

从账号密码拿到 CASTGC cookie

import nju_login
response = nju_login.login('学号', '统一认证密码')

print(response.cookies["CASTGC"])

安装

pip install nju-login-simple

登陆南大网站

正常使用浏览器时,南大SSO的登陆流程一般是:

  1. 你去访问目标网站,比如课表 https://ehall.nju.edu.cn/appShow?appId=4770397878132218
  2. 目标网站发现你没登陆,于是给你重定向到统一认证
  3. 你在统一认证完成登陆,它给你重定向回目标网站,此时URL会带上一个ticket
  4. 目标网站根据ticket给你设置cookie,然后重定向到你一开始想去的页面

这里面只有第三步需要人工参与,其它地方都是自动重定向。

本项目要做的就是帮你跳过第三步。如果你访问统一认证网站时带上了有效的CASTGC cookie,那么它就会知道你已经登陆,并直接给你ticket然后带你重定向。

因此,如果想借助本项目登陆南大网站(比如ehall或者epay啥的),一般流程是:

  1. 调用login,拿到castgc
  2. 将castgc加到自己的request session的cookies里。此时你的session在统一认证有登陆,在服务站没登陆。
  3. 用自己的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.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for nju-login-simple 1.2.1
File Size Uploaded Provenance Actions
nju_login_simple-1.2.1.tar.gz 6.5 kB With provenance Details for nju_login_simple-1.2.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for nju-login-simple 1.2.1
File Interpreter ABI Platform Actions
nju_login_simple-1.2.1-py3-none-any.whl Python 3 none any Details for nju_login_simple-1.2.1-py3-none-any.whl

Total release size: 13.5 kB

Release files / nju_login_simple-1.2.1.tar.gz

Download URL nju_login_simple-1.2.1.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
5e6a126b9df1789da2781d146541cd670d19068d2122ae50029bd24ae7b0e7d5
BLAKE2b-256 checksum
How to use checksums
e3748ef734d42da6163039c2c508a096582ca6b4ca5b3d94d2feaa0b99fcabb2
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 Sep 6, 2026.

Transparency log

Release files / nju_login_simple-1.2.1-py3-none-any.whl

Download URL nju_login_simple-1.2.1-py3-none-any.whl
Size 7.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c0568071356c019022d999c27e8c2ab74cdc84ad620d28380424458e55279d8e
BLAKE2b-256 checksum
How to use checksums
d66aa6cbc7d72926c02b7d25b5405c17b9e9815bb0c9c5f5e8fcd9f824017283
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 Sep 6, 2026.

Transparency log

Security for nju-login-simple 1.2.1

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.

Select from 2 files

Attestation for nju_login_simple-1.2.1.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 Sep 6, 2026.

Transparency log

A 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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page