-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathadmin_api_get_channels_on_server.py
65 lines (52 loc) · 2.35 KB
/
admin_api_get_channels_on_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import argparse
import requests
import sys
import json
# Function for executing HTTP request (channel creation)
def get_server_channels(backchannel_address, Lkey):
# HTTP header. Indicates the MIME document type (json) as well as the authorization key
headers = {
'accept': 'application/json',
'Content-Type': 'application/json',
'Authorization': 'LKey %s' % str(Lkey),
}
address = str(backchannel_address) + '/api/v3/channels/' # Server address
# Exclusion block. If a request error occurs during the sending of the request (for example, the server is not available), the program displays the error status and terminates the execution.
try:
response = requests.get(address, headers=headers)
# print (response.text)
except requests.exceptions.RequestException as e:
print (e)
sys.exit(1)
return response.status_code, response.text # The function returns the status of the HTTP request sending code
# Parsing program call arguments
parser = argparse.ArgumentParser()
parser.add_argument('-serv_backchannel', '--serv_backchannel', help = 'VXG Server address', required=True)
parser.add_argument('-serv_key', '--vxg_serv_key', help = 'VXG Server key', required=True)
# Block responsible for checking the entered parameters. In case of an error, the status and description of use are returned
try:
param = parser.parse_args()
except IOError as msg:
parser.error(str(msg))
# Function call to send an HTTP request
code, data = get_server_channels(param.serv_backchannel, param.vxg_serv_key)
# Convertion to json
try:
data_json = json.loads(data)
except Exception:
pass
# Output status of HTTP request.
print ('Request completed. HTTP status code: ' + str(code)+'\n')
# Output response content
try:
print('Number of cameras on server: ' + str(data_json['meta']['total_count']) + '\n')
for channel in data_json['objects']:
print(' Camera ID: ' + str(channel['id']) + '\n')
print(' Created: ' + str(channel['created']) + '\n')
print(' Name: ' + channel['name'] + '\n')
print(' Recording Mode: ' + channel['rec_mode'] + '\n')
print(' streaming: ' + str(channel['streaming']) + '\n')
print(' Time Zone: ' + channel['timezone'] + '\n')
print(' ----------------------------------')
except Exception:
print(data)