-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmxapi.py
More file actions
142 lines (124 loc) · 4.79 KB
/
Copy pathmxapi.py
File metadata and controls
142 lines (124 loc) · 4.79 KB
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
# ****************************************************************************
# * mxapi.py
# * Mobotix api sender
#
# This script sends http api comands to (multiple) mobotix camera's
# usage:
# python mxapi.py [options]
# use option -h or --help for instructions
#
# release info
# 1.0 first release 140520 Paul Merkx
# ****************************************************************************
import os
import requests
import sys
import argparse
import csv
import io
RELEASE = '1.0 - 14 may 2020'
TIMEOUT = 3 # requests timeout
def validate_ip(s):
a = s.split('.')
if len(a) != 4:
return False
for x in a:
if not x.isdigit():
return False
i = int(x)
if i < 0 or i > 255:
return False
return True
# ***************************************************************
# *** Main program ***
# ***************************************************************
print('MxProgram ' + RELEASE + ' by (c) Simac Healthcare.')
print('Disclaimer: ')
print('USE THIS SOFTWARE AT YOUR OWN RISK')
print(' ')
# *** Read arguments passed on commandline
parser = argparse.ArgumentParser()
parser.add_argument("-d", "--deviceIP", nargs=1, help="specify target device IP when programming a single camera")
parser.add_argument("-l", "--devicelist", nargs=1, help="specify target device list in CSV when programming multiple camera's")
parser.add_argument("-a", "--apicommand", nargs=1, help="specify api command to send to camera(s).")
parser.add_argument("-u", "--username", nargs=1, help="specify target device admin username")
parser.add_argument("-p", "--password", nargs=1, help="specify target device admin password")
parser.add_argument("-s", "--ssl", help="use SSL to communicate (HTTPS)", action="store_true")
parser.add_argument("-t", "--timeout", nargs=1, help="specify cUrl timeout in seconds (default = 60)")
args = parser.parse_args()
# *** Check validity of the arguments
if (args.deviceIP is None and args.devicelist is None) or (args.deviceIP and args.devicelist):
print("Either deviceIP or devicelist is required")
sys.exit()
if args.username is None:
print("Default Admin account assumed")
username = 'admin'
else:
username = args.username[0]
if args.password is None:
print("Default Admin password assumed")
password = 'meinsm'
else:
password = args.password[0]
if args.timeout:
try:
TIMEOUT = int(args.timeout[0])
except:
print("Unable to understand timeout value of " + args.timeout[0])
print("Try an interger")
sys.exit()
if args.deviceIP:
if not validate_ip(args.deviceIP[0]):
print("The device %s is not a valid IPv4 address!" % (args.deviceIP[0]))
sys.exit()
if args.devicelist:
if not os.path.exists(args.devicelist[0]):
print("The devicelist '%s' does not exist in the current directory!" % (args.devicelist[0]))
sys.exit()
if not args.apicommand:
print("The program requires an apicommand parameter! (like '-a /control/rcontrol')")
print("Look at https://community.mobotix.com/t/getting-started-with-the-http-api/52 for more info")
print("or in the help of the camera: http://<ip_address_for_the_camera>/help/help at to bottom of the page")
sys.exit()
if args.ssl:
use_ssl = True
else:
use_ssl = False
print('Starting')
print('Build devicelist...')
# Build devicelist from devicelist file or from single parameter
# devicelist is a list of lists
devicelist = []
if args.devicelist:
csv.register_dialect('semicolons', delimiter=';')
with open(args.devicelist[0], 'r') as f:
reader = csv.reader(f, dialect='semicolons')
for row in reader:
devicelist.append(row)
else:
devicelist.append(['IP'])
devicelist.append([args.deviceIP[0]])
#devicelist[0] now contains the header
for devicenr in range(1, len(devicelist)): #skip header
#skip device if starts with comment
if devicelist[devicenr][0][0] != '#':
ipaddr = devicelist[devicenr][0]
print('About to program device ' + ipaddr + ' ', end='')
if use_ssl:
proto = "https://"
else:
proto = "http://"
try:
r = requests.get(proto + ipaddr + args.apicommand[0], auth=(username, password), timeout=TIMEOUT, verify=False)
r.raise_for_status()
if r.status_code == 200:
print('...OK')
except requests.exceptions.HTTPError as errh:
print ("... Fail. Http Error:",errh)
except requests.exceptions.ConnectionError as errc:
print ("... Fail. Error Connecting:",errc)
except requests.exceptions.Timeout as errt:
print ("... Fail. Timeout Error:",errt)
except requests.exceptions.RequestException as err:
print ("... Fail. Something weird happened ",err)
print("Done.")