海康安防平台api的python调用两种方式

本文档展示了如何使用Python通过两种方式调用海康的Java SDK以及直接进行加密操作。第一种方法是利用`jpype`库直接在Python中启动Java虚拟机并调用SDK;第二种方法是实现AK/SK签名认证,直接在Python中完成加密请求。内容涉及到HTTPS协议、时间戳、UUID、HMAC和hashlib等技术。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

海康给了java的sdk,c++的,瞧不起我py,居然没有py的sdk。话说视频流+深度学习应用,py难道不是主流?废话不多说,有两个方式调用:

python 调用java

import jpype
import jpype.imports
from jpype.types import *
import os, json
import requests


class CallJava:
    def __init__(self):
        # java调用不可以加https://
        self.host = "xx.xx.xx.xx:xxxx"
        self.key = "xxxx"
        self.secret = "xxxx"
        self.jar = os.path.abspath("./artemis-http-client-1.1.3.jar")
    
    def call_java(self):
        jpype.startJVM(classpath=[self.jar])
        from com.hikvision.artemis.sdk import ArtemisHttpUtil
        from com.hikvision.artemis.sdk.config import ArtemisConfig

        jpype.java.lang.System.out.println("java_HelloWorld")

        ArtemisConfig.host = self.host
        ArtemisConfig.appKey = self.key
        ArtemisConfig.appSecret = self.secret
        
        previewURLsApi = jpype.java.util.HashMap()
        previewURLsApi.put("https://", "/artemis/api/video/v1/pictureInfos")
        
        contentType = "application/json"
        
        jsonBody = {"pageNo": 1,
                    "pageSize": 400,
                    "startTime": "2020-07-26T15:00:00.000+08:00",
                    "endTime": "2021-07-26T15:00:00.000+08:00", }
        body = json.dumps(jsonBody)
        
        result = ArtemisHttpUtil.doPostStringArtemis(previewURLsApi, body, None, None, contentType, None)
        print(result)
        print("接口调用完毕")


if __name__ == '__main__':
    c = CallJava()
    c.call_java()

python直接自己加密

# -*- coding: utf-8 -*-
# _author_ =
# Email:
"""
协议:仅支持 HTTPS 传输
url格式:https://{hostname}:{port}/artemis/{uri}
# AK\SK摘要认证
调用 API 时,如果API需要安全认证,首先需要获取API的授权,得到AppKey和AppSecret;
其次,拼接签名字符串,将计算后的签名放在请求的 Header 传入,网关会通过对称计算签名来验证请求者的身份。
"""

import os
import base64
import json
import time
import uuid
import hmac  # hex-based message authentication code 哈希消息认证码
import hashlib  # 提供了很多加密的算法
import requests


class HaiKang:
    def __init__(self):
        self.base_url = "https://xxxxxxxxxxxxxxx"  # 可以正常访问的IP地址
              
        self.appKey = "xxxxxxxxxxxxxx"
        self.appSecret = "xxxxxxxxxxxxxxxxx"
        
    
    def headers(self, api_get_address_url="/artemis/api/video/v2/cameras/previewURLs"):
        x_ca_nonce = str(uuid.uuid4())
        x_ca_timestamp = str(int(round(time.time()) * 1000))
        # sign_str 的拼接很关键,不然得不到正确的签名
        sign_str = "POST\n*/*\napplication/json" + "\nx-ca-key:" + self.appKey + "\nx-ca-nonce:" + \
                   x_ca_nonce + "\nx-ca-timestamp:" + x_ca_timestamp + "\n" + \
                   api_get_address_url
        
        temp = hmac.new(self.appSecret.encode(), sign_str.encode(), digestmod=hashlib.sha256)
        signature = base64.b64encode(temp.digest()).decode()
        print("[INFO] 获取到的签名值为:", signature)
        
        headers = {
            "Accept": "*/*",
            "Content-Type": "application/json",
            "x-ca-key": self.appKey,  # appKey,即 AK
            "x-ca-signature-headers": "x-ca-key,x-ca-nonce,x-ca-timestamp",
            "x-ca-signature": signature,  # 需要计算得到的签名,此处通过后台得到
            "x-ca-timestamp": x_ca_timestamp,  # 时间戳
            "x-ca-nonce": x_ca_nonce  # UUID,结合时间戳防重复
        }
        return headers
    
    # 获取监控点的在线流样例
    def examples(self,api_get_address_url="/artemis/api/video/v2/cameras/previewURLs"):
        headers=self.headers(api_get_address_url)
        body = {
            "cameraIndexCode": "36e0421391f7****",
            "streamType": 0,
            "protocol": "rtsp",
        }
        url = self.base_url + api_get_address_url
        results = requests.post(url, data=json.dumps(body), headers=headers, verify=False)
        print(results)
        print(results.json())
        # {'code': '0', 'msg': 'success', 'data': {'url': 'rtsp://ip:554/openUrl/Kp0VqXS'}}
    def pics(self):
        api_get_address_url="/artemis/api/video/v1/pictureInfos"
        headers=self.headers(api_get_address_url)
        body = {
            "pageNo": 1,
            "pageSize": 400,
            "startTime": "2020-07-26T15:00:00.000+08:00",
            "endTime": "2021-07-26T15:00:00.000+08:00",
        }
        url = self.base_url + api_get_address_url
        results = requests.post(url, data=json.dumps(body), headers=headers, verify=False)
        print(results)
        print(results.json())

if __name__ == '__main__':
    c=HaiKang()
    c.pics()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值