Skip to main content

NJUlogin

  • 南京大学统一身份认证登录模块,可用于登录校园各种网站。

GitHub PyPI License: MIT

安装

注:由于使用了新的打包方式以及一些其他原因,自v3.3起,python依赖调整为python>=3.10,<4.0,并且在pypi上删除所有旧版包。安装旧版方法(二选一):

  • Github releases中下载对应版本的.whl文件并pip install xxx.whl
  • 克隆Github仓库并且checkout到对应tag的分支后使用pip install .安装。

从 PyPI 安装(推荐)

pip install NJUlogin

从源码安装

git clone https://github.com/Do1e/NJUlogin.git
cd NJUlogin
pip install .
# Or `pip install -e .` for editable mode

aur

如果你使用 Arch Linux 或基于 Arch 的发行版,可以通过 AUR 安装:

yay -S python-njulogin

使用

代码调用

  • 包含三种登录方法,扫码登录账号密码登录加载cookies登录,使用方法见demos文件夹

  • 扫码登录:构造QRlogin对象即可调用login方法进行登录。会在终端打印统一身份验证的二维码,使用手机扫码登录即可。(未测试字体,若出问题请尝试更换终端字体,如MesloLGS NFFira Code,也会在当前目录保存图片文件作为备选方案)
  • 账号密码登录:使用账号密码作为参数构造pwdLogin对象即可调用login方法进行登录。新版滑块验证码使用纯 OpenCV 图像处理自动定位缺口。
    • pwdLogin(username, password, mobile=True):传入mobile=True可使用移动端登录方式(移动端UA + dllt=mobileLogin),能够获取较为长效的认证信息,适合需要长期保持登录状态的场景。
  • 加载cookies登录:构造baseLogin对象即可调用load方法加载cookies,cookies需要通过上述两种登录方式后使用export方法导出为文件。loadexport方法可以设置保存文件的密码防止泄露。
  • login方法需要传入登录的目的网址,比如https://p.nju.edu.cn/api/cas/getinfo表示登录到校园网,返回的网页会保存在self.response中。目的网址也可以留空。
  • 目的网址获取方法(举一反三即可):打开浏览器输入p.nju.edu.cn,会发现自动跳转到https://authserver.nju.edu.cn/authserver/login?service=https://p.nju.edu.cn/api/cas/getinfo,即为service=后面的内容。
  • 返回值session记录了登录状态,之后即可使用requests中的方法进行进一步的操作,也可以使用构造出的对象调用getpost方法。(具体能有什么操作就看各位的创意了,也可以查看我的示例

方法/属性列表:

  • login(self, dest: str = None):登录
  • get(self, url: str, **kwargs) -> requests.Response:重载了requests.get方法
  • post(self, url: str, data: dict, **kwargs) -> requests.Response:重载了requests.post方法
  • logout(self):退出登录
  • logout_all(self):退出所有设备的登录
  • available:判断是否登录成功
  • export(self, filename: str, password: str = None):导出cookies,推荐在公用机器上使用环境变量设置文件密码
  • load(self, filename: str, password: str = None):加载cookies,推荐在公用机器上使用环境变量设置文件密码

cli登录p.nju

使用curl命令可以简洁实现:

# 登录
curl https://p.nju.edu.cn/api/portal/v1/login -X POST -d '{"username":"学号", "password":"密码"}'
# 登出
curl https://p.nju.edu.cn/api/portal/v1/logout -X POST -d '{}'

v3.4起集成了 https://p.nju.edu.cn 的登录客户端,使用方法如下:

# 安装后
NJUlogin -h
# 或者
uvx NJUlogin -h
usage: NJUlogin [-h] [-l loginlib] [-i] [-o] [-k online_id] [-p] [-a name] [-d device_id]

options:
  -h, --help            show this help message and exit
  -l, --loginlib loginlib
                        登录库,QRlogin或pwdLogin,默认为QRlogin
  -i, --login           登录
  -o, --logout          登出
  -k, --kick online_id  登出指定在线设备,使用`-p`可获取online_id
  -p, --printinfo       打印信息
  -a, --add name        添加当前设备为无感认证设备,name为设备名称
  -d, --delete device_id
                        删除无感认证设备,指定设备ID,使用`-p`可获取device_id

补充

  • 这个项目很难进行完整的测试,毕竟难以预测所有的网络情况,而且网站的登录方式也会有更新,因此欢迎大家提出issue,我会尽力解决(只要我还在南大)。

开源许可

本项目采用 MIT 开源许可证。

免责声明

  • 本项目仅供学习交流使用,不得用于商业用途,如有侵权请联系删除
  • 用户使用本项目所产生的任何后果,需自行承担风险
  • 开发者不对使用本项目产生的任何直接或间接损失负责

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

njulogin-5.1.1.tar.gz (15.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

njulogin-5.1.1-py3-none-any.whl (18.5 kB view details)

Uploaded Python 3

File details

Details for the file njulogin-5.1.1.tar.gz.

File metadata

  • Download URL: njulogin-5.1.1.tar.gz
  • Upload date:
  • Size: 15.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for njulogin-5.1.1.tar.gz
Algorithm Hash digest
SHA256 0cc1d967b48fbed6efe21b5ef7200f70d5be4b6ba58475e759c1f17ca7f8aa60
MD5 bb31d76119b1a3244e66ef0766ea929e
BLAKE2b-256 53a3868ffd6086c2a24592a3054b8acfc98b85151498c0a3e6ae17abb0d697d8

See more details on using hashes here.

File details

Details for the file njulogin-5.1.1-py3-none-any.whl.

File metadata

  • Download URL: njulogin-5.1.1-py3-none-any.whl
  • Upload date:
  • Size: 18.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.32 {"installer":{"name":"uv","version":"0.11.32","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Arch Linux","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for njulogin-5.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 e2a95d87934736f68d6312796f9d24e0fe5ce9a1ce64347529a47dc212cd53b9
MD5 b37089c53ced4e4faa77eb0e3bf112d5
BLAKE2b-256 99b53fb9db77b3115ee4dd508d8b00d84f58b277c021d5ff0ef8cda388bc12de

See more details on using hashes here.

Release history Release notifications | RSS feed

This release

5.1.1 This release

2 files

5.1.0

2 files

5.0.0

2 files

4.0.1

2 files

4.0.0

2 files

3.6.1

2 files

3.5.1

2 files

3.5.0

2 files

3.4.3

2 files

3.4.2

2 files

3.4.1

2 files

3.4.0

2 files

3.3

2 files

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page