py-hikvision-toolkit
一个用于与海康威视 iSecure Center (ISC) API 交互的 Python 客户端库。
项目主页
Gitee: https://gitee.com/guolei19850528/py_hikvision_toolkit
文档: 海康威视开放平台
功能特性
- HMAC-SHA256 签名算法:符合 Artemis 网关规范的请求签名
- 同步/异步支持:同时支持同步和异步 HTTP 请求
- 请求头自动生成:自动处理认证信息(Access Key、Nonce、Timestamp、Signature)
- 响应数据模型:基于 Pydantic 的响应数据验证和类型提示
- 工具函数集:提供图片处理、URL 处理、响应模型构建等辅助功能
安装
使用 uv(推荐)
uv add py-hikvision-toolkit
使用 pip
pip install py-hikvision-toolkit
快速开始
from py_hikvision_toolkit.isc import Isc
from py_hikvision_toolkit.isc.responses import build_base_instance
from py_hikvision_toolkit.isc.utils import url_add_artemis_prefix
# 初始化 ISC 客户端
isc = Isc(
host="https://isc.example.com:1443",
ak="your_access_key",
sk="your_secret_key"
)
# 发送同步请求
response = isc.request_with_headers(
url=url_add_artemis_prefix("/api/resource/v1/org/orgInfo"),
json={"orgIndexCodes": ["root000000"]}
)
# 解析响应
result = build_base_instance(response)
if result.is_success():
print(result.data)
else:
print(f"请求失败,错误码:{result.code},错误信息:{result.msg}")
API 说明
Isc 客户端类
初始化
from py_hikvision_toolkit.isc import Isc
isc = Isc(
host="https://isc.example.com:1443", # ISC 服务器地址
ak="your_ak", # Access Key
sk="your_sk", # Secret Key
client_kwargs={"timeout": 30} # 可选:客户端配置
)
核心方法
| 方法 | 说明 | 参数 |
|---|---|---|
signature(string) |
生成 HMAC-SHA256 签名 | string: 待签名字符串 |
headers(method, path, headers) |
生成认证请求头 | method: HTTP 方法, path: 请求路径, headers: 额外请求头 |
request_with_headers(**kwargs) |
发送同步请求 | 支持 httpx.Client.request 的所有参数 |
async_request_with_headers(**kwargs) |
发送异步请求 | 支持 httpx.AsyncClient.request 的所有参数 |
认证机制
ISC API 使用基于 HMAC-SHA256 的签名机制:
-
构建签名字符串(按顺序拼接):
- HTTP 方法(如 POST)
- Accept 头值
- Content-Type 头值
- x-ca-key
- x-ca-nonce(随机 UUID)
- x-ca-timestamp(毫秒时间戳)
- 请求路径
-
使用 Secret Key 对签名字符串进行 HMAC-SHA256 签名
-
将签名放入
x-ca-signature请求头中
utils 工具函数
| 函数 | 说明 | 参数 | 返回值 |
|---|---|---|---|
timestamp() |
生成毫秒时间戳 | - | int |
nonce() |
生成随机 UUID(无连字符) | - | str |
url_add_artemis_prefix(url) |
添加 Artemis 前缀 | url: 请求路径 |
str |
build_base_instance(response) |
构建 Base 模型 | response: HTTP 响应或字典 |
Base |
image_to_base64_and_md5(image_path) |
图片转 Base64 和 MD5 | image_path: 图片路径 |
Tuple[str, str] |
build_success_instance(response) |
构建 Success 模型 | response: HTTP 响应或字典 |
Success |
success_is_valid(response, schema) |
校验成功响应 | response: 响应数据, schema: Schema |
bool |
响应数据模型
Base 模型
from py_hikvision_toolkit.isc.responses import Base
response = {"code": 0, "msg": "success"}
base = Base(**response)
字段说明:
code: 错误码,0 表示成功(支持整数和字符串)msg: 错误信息描述
Success 模型
from py_hikvision_toolkit.isc.responses import Base
response = {"code": 0, "msg": "success", "data": {"items": []}}
success = Base(**response)
print(success.is_success())
字段说明:
code: 固定为 0 或 "0"msg: 成功消息(可选)data: 返回的实际数据
示例代码
示例 1:获取组织信息
from py_hikvision_toolkit.isc import Isc
from py_hikvision_toolkit.isc.responses import build_base_instance
from py_hikvision_toolkit.isc.utils import url_add_artemis_prefix
isc = Isc(host="https://isc.example.com:1443", ak="your_ak", sk="your_sk")
response = isc.request_with_headers(
url=url_add_artemis_prefix("/api/resource/v2/org/advance/orgList"),
json={"orgIndexCodes": "root000000", "pageNo": 1, "pageSize": 100}
)
result = build_base_instance(response)
if result.is_success():
print(result.data)
else:
print(f"请求失败,错误码:{result.code},错误信息:{result.msg}")
示例 2:异步请求
import asyncio
from py_hikvision_toolkit.isc import Isc
from py_hikvision_toolkit.isc.responses import build_base_instance
async def main():
isc = Isc(host="https://isc.example.com:1443", ak="your_ak", sk="your_sk")
response = await isc.async_request_with_headers(
url="/api/resource/v1/org/orgInfo",
json={"orgIndexCodes": ["root000000"]}
)
result = build_base_instance(response)
if result.is_success():
print(result.data)
else:
print(f"请求失败,错误码:{result.code},错误信息:{result.msg}")
asyncio.run(main())
示例 3:添加人脸
from py_hikvision_toolkit.isc import Isc
from py_hikvision_toolkit.isc.utils import url_add_artemis_prefix, image_to_base64_and_md5
isc = Isc(host="https://isc.example.com:1443", ak="your_ak", sk="your_sk")
# 将图片转换为 Base64
base64_str, md5_str = image_to_base64_and_md5("/path/to/face.jpg")
response = isc.request_with_headers(
url=url_add_artemis_prefix("/api/resource/v1/person/single/add"),
json={
"tagId": "frs",
"personName": "张三",
"certificateType": "111",
"certificateNo": "110101199001011234",
"orgIndexCode": "e5b44598-9b9f-4273-8540-3209eaf4c7a1",
"faces": [{"faceData": base64_str}]
}
)
项目结构
py_hikvision_toolkit/
├── src/
│ └── py_hikvision_toolkit/
│ ├── __init__.py
│ └── isc/
│ ├── __init__.py # Isc 客户端类
│ ├── responses.py # 响应数据模型
│ └── utils.py # 工具函数
├── pyproject.toml
├── LICENSE
└── README.md
许可证
MIT License
作者
Guolei (174000902@qq.com)
Metadata
Release files for py-hikvision-toolkit 1.0.5
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 | |
|---|---|---|---|---|
| py_hikvision_toolkit-1.0.5.tar.gz | 13.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| py_hikvision_toolkit-1.0.5-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 28.7 kB
Release files / py_hikvision_toolkit-1.0.5.tar.gz
| Download URL | py_hikvision_toolkit-1.0.5.tar.gz |
|---|---|
| Size | 13.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
1e5e1afb7b034e15eec9f09885a438a91ceb89a5d2e7877eaa5a5162e5beea1d
|
|
BLAKE2b-256 checksum How to use checksums |
21a40fe7aa17c91014d1373e8bd28122e0ea3141e232c2d67c942510e59bb388
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.11
|
Release files / py_hikvision_toolkit-1.0.5-py3-none-any.whl
| Download URL | py_hikvision_toolkit-1.0.5-py3-none-any.whl |
|---|---|
| Size | 14.9 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
5a84adfe66184a47cc352c46207b41681cb9c1477e66d7c5e8a70e94d69037b7
|
|
BLAKE2b-256 checksum How to use checksums |
0e9a6f6e6b093a642da78ca254a40afb2e919e24853bf49599b195422e8c2e9e
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.6.11
|
Security for py-hikvision-toolkit 1.0.5
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.
No provenance found
None of the files in this release were uploaded with provenance metadata.
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.