-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathRestClient.py
More file actions
executable file
·64 lines (55 loc) · 2.05 KB
/
RestClient.py
File metadata and controls
executable file
·64 lines (55 loc) · 2.05 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
#!/usr/bin/env python
import requests
import json
import sys
import io
import os
from optparse import OptionParser
from requests.auth import HTTPBasicAuth
def main(opts):
if not opts.url or not opts.restcmd:
p.error("to specify url and restcmd is minimum")
url = opts.url
username = opts.username
password = opts.password
restcmd = getattr(requests, opts.restcmd)
jheader = {'Content-Type': 'application/json'}
try:
if opts.jsoncfg:
jsoncfg = json.load(
io.open(opts.jsoncfg, mode="r", encoding="utf-8"))
r = restcmd(url, auth=HTTPBasicAuth(
username, password), json=jsoncfg, verify=opts.tls)
else:
r = restcmd(url, auth=HTTPBasicAuth(
username, password), headers=jheader, verify=opts.tls)
if restcmd == requests.delete:
print("\n")
print("deleted %s with statuscode of %s") % (url, r.status_code)
elif r.ok:
jdata = json.loads(r.content)
print("\n")
print(json.dumps(jdata, indent=4, sort_keys=True))
else:
r.raise_for_status()
except Exception as e:
print(e)
print(r.text)
sys.exit(1)
except:
print ("Connection error")
sys.exit(1)
if __name__ == '__main__':
p = OptionParser(usage="usage: %prog [ -r post ] -U resturl -j jsonfile",
version="%prog 0.9")
p.add_option("-u", "--user", dest="username", help="Username for restapi")
p.add_option("-U", "--url", dest="url",
help="Url of the rest api: https://<fqdn>/<section>")
p.add_option("-p", "--password", dest="password",
help="Password for restapi")
p.add_option("-r", "--restcmd", dest="restcmd",
help="get, post, patch, or delete command")
p.add_option("-j", "--jsoncfg", dest="jsoncfg", help="jsoncfg as file")
p.add_option("-t", "--tls-verify", dest="tls", action="store_false", help="tls verify")
(opts, args) = p.parse_args()
main(opts)