python獲取海康威視所有攝像頭的OSD通道名稱
-
讀取IP地址的txt文檔
-
根據IP地址獲取監控攝像頭的OSD通道名稱
# coding=utf-8
import os
import time
import requests
from requests.auth import HTTPBasicAuth, HTTPDigestAuth
import xml.etree.ElementTree as ET
#注意:和ip.txt放在一個文件夾,會生成ip_name.txt文件#根據ip地址清單,獲取攝像頭的信息
#和監控攝像頭通訊需要一個雙方認可的密鑰,可以隨機生成
def generate_key():# 生成一個16字節的隨機字節數組,16字節對應128位random_bytes = os.urandom(16)# 將字節數組轉換成十六進制字符串hex_key = random_bytes.hex()return hex_key
def fun_GetOSD_Name(url):# 嘗試使用Basic Auth登錄session = requests.Session()session.auth = HTTPDigestAuth(USERNAME, PASSWORD)try:# 發送GET請求response = session.get(url)if response.status_code == 200:# 解析XML響應以獲取OSD通道名稱osd_config = ET.fromstring(response.text)#print("OSD Configuration:", osd_config)else:print("Failed to retrieve OSD configuration. Status code:", response.status_code)# 找到并打印攝像頭的OSD-name元素的文本name_element = osd_config.find('{http://www.hikvision.com/ver20/XMLSchema}name')if name_element is not None:osd_name = name_element.textreturn osd_nameelse:print("Name element not found")return '沒找到通道名稱'except Exception as e:print("An error occurred:", e)def get_ip_list():#從txt中獲得ip列表,根據列表獲得攝像頭信息并,存入txt中# 文件路徑file_path =f'{os.getcwd()}/ip.txt'# 創建一個空列表來存儲每一行的數據data_list = []try:# 打開文件with open(file_path, 'r', encoding='utf-8') as file:# 逐行讀取文件內容for line in file:# 去除行尾的換行符(\n 或 \r\n),然后添加到列表中data_list.append(line.strip())except FileNotFoundError:print(f"Error: The file {file_path} does not exist.")except Exception as e:print(f"An error occurred: {e}")# 打印列表,驗證是否正確讀取了文件內容print(data_list)return data_listif __name__=='__main__':USERNAME = 'admin'PASSWORD = 'qlyy1234'ip_list=[]ip_list=get_ip_list()#跳轉1:ip_name_list=[]for ip in ip_list:HOST=ipasekey=generate_key()#url1:輸出格式的地址;url2:輸出OSD名字的地址,后邊的密鑰可以是任意值url1=f'http://{HOST}/ISAPI/System/Video/inputs/channels/1/overlays'url2=f'http://{HOST}/ISAPI/System/Video/inputs/channels/1/?security=1&iv={asekey}'#獲取通道名稱Name=fun_GetOSD_Name(url2)ip_name=f'{ip}\t{Name}'ip_name_list.append(ip_name)print(ip_name)time.sleep(0.2)#寫入文本# 文件路徑file_path =f'{os.getcwd()}/ip_name.txt'# 如果文件存在,則先重命名if os.path.exists(file_path):new_file_path = file_path + '.bak'os.rename(file_path, new_file_path)try:# 打開文件,如果不存在則會被創建with open(file_path, 'w', encoding='utf-8') as file:# 寫入列表中的每個元素,每行一個for ip in ip_name:file.write(ip + '\n')except IOError as e:print(f"An error occurred while writing to the file: {e}")else:print(f"Successfully wrote data to {file_path}")# 如果之前重命名了文件,可以在這里做進一步處理,例如刪除舊文件或記錄日志
本文來自互聯網用戶投稿,該文觀點僅代表作者本人,不代表本站立場。本站僅提供信息存儲空間服務,不擁有所有權,不承擔相關法律責任。
如若轉載,請注明出處:http://www.pswp.cn/diannao/42459.shtml
繁體地址,請注明出處:http://hk.pswp.cn/diannao/42459.shtml
英文地址,請注明出處:http://en.pswp.cn/diannao/42459.shtml
如若內容造成侵權/違法違規/事實不符,請聯系多彩編程網進行投訴反饋email:809451989@qq.com,一經查實,立即刪除!