import requests import json import datetime import random from Utils.HikAccessToken import hik_access_token def datetime_now(): datetimenow = datetime.datetime.now().strftime("%Y%m%d%H%M%S") return datetimenow def getcardNo(): all_chars = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ' captcha = '' for _ in range(15): a = random.choice(all_chars) captcha += a # 连接字符串 return captcha def hik_getcard(jobids): card_lists = [] for i in jobids: if i is not None: card = { "cardNo": getcardNo(), "cardType": "normalCard", "employeeNo": i } card_lists.append(card) return card_lists def hik_addcard(hik_accesstoken,jobids): headers = {"content-type": "application/json"} data = { "cards": hik_getcard(jobids) } print(data) response = requests.post( url="https://api2.hik-cloud.com/api/v1/open/basic/cards/batchCreate?access_token=%s" % hik_accesstoken, headers=headers, data=json.dumps(data) ) print(response.json()) if response.json()["code"] == 200: print("%s,创建卡片成功"%(datetime_now())) else: print(datetime_now(),response.json()["message"]) # return response.text #if __name__ == '__main__': #with open("../config.json") as f: # data = json.load(f) # appkey = data["appkey"] # appsecret = data["appsecret"] # client_id = data["client_id"] # client_secret = data["client_secret"] # groupId = data["groupId"] # sleeptime = data["sleeptime"] # 获取Hik token # hik_accesstoken = hik_access_token(client_id, client_secret) # print(hik_accesstoken) # diff_value = ["2900","2893"] # hik_addcard(hik_accesstoken, list(diff_value))