From 77b80dd9b3500539b24b7dc6258c02c23fd4d015 Mon Sep 17 00:00:00 2001 From: Evgeny Zinoviev Date: Sat, 17 Feb 2024 13:39:01 +0300 Subject: fix ipcam_ntp_util --- bin/ipcam_ntp_util.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) (limited to 'bin/ipcam_ntp_util.py') diff --git a/bin/ipcam_ntp_util.py b/bin/ipcam_ntp_util.py index 0268a06..b27995c 100755 --- a/bin/ipcam_ntp_util.py +++ b/bin/ipcam_ntp_util.py @@ -151,6 +151,23 @@ class HikvisionISAPIClient: ntp_port: int = 123): format = 'ipaddress' if validate_ipv4(ntp_host) else 'hostname' + # test ntp server first + data = f'{format}{ntp_host}{ntp_port}' + r = requests.post(self.isapi_uri('System/time/ntpServers/test'), data=data, headers={ + 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8', + 'X-Requested-With': 'XMLHttpRequest', + }, cookies=self.cookies) + r.raise_for_status() + + resp = xml_to_dict(r.text)['NTPTestResult'] + + error_code = int(resp['errorCode'][0]) + error_description = resp['errorDescription'][0] + + if error_code != 0 or error_description.lower() != 'ok': + raise ResponseError('response status looks bad') + + # then set it data = '' data += f'1{format}{ntp_host}{ntp_port}1440' @@ -194,6 +211,7 @@ def process_hikvision_camera(host: str, print(f'[{host}] {client.get_ntp_server()}') return client.set_ntp_server(ntp_server) + print(f'[{host}] done') except AuthError as e: print(f'[{host}] ({str(e)})') except ResponseError as e: -- cgit v1.2.3