-
Notifications
You must be signed in to change notification settings - Fork 0
/
HTTPMethod.py
executable file
·66 lines (63 loc) · 1.98 KB
/
HTTPMethod.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
66
#!/usr/bin/python3
import json
import urllib.request
import ssl
import sys
class HTTPMethod():
def __init__(self,ip):
self.url = 'https://{}/redfish/v1'.format(ip)
self.id = ""
self.token = ""
def MakeRequest(self,req):
context = ssl._create_unverified_context()
try:
res = urllib.request.urlopen(req,context=context)
header = res.info()
response = res.read().decode('utf-8')
return header,response
except urllib.request.HTTPError as e:
print(e.read())
def post(self,url,data):
url = self.url + str(url)
data = json.dumps(data).encode('utf-8')
req = urllib.request.Request(url,data)
req.get_method = lambda: "POST"
req.add_header('Content-Type','application/json')
header, res = self.MakeRequest(req)
self.id = header.get('Location').split('/')[-2]
self.token = header.get('X-Auth-Token')
res = json.dumps(json.loads(res),indent=4,sort_keys=True)
return header,res
def get(self,url):
url = self.url + str(url)
headers={
'Content-Type' : 'application/json','X-Auth-Token' : self.token
}
req = urllib.request.Request(url,headers=headers)
req.get_method = lambda: "GET"
header, res = self.MakeRequest(req)
res = json.dumps(json.loads(res),indent=4,sort_keys=True)
return header,res
def delete(self,url):
url = self.url + str(url) + str(self.id) + "/"
headers={
'Content-Type' : 'application/json','X-Auth-Token' : self.token
}
req = urllib.request.Request(url,headers=headers)
req.get_method = lambda: "DELETE"
header, res = self.MakeRequest(req)
res = json.dumps(json.loads(res),indent=4,sort_keys=True)
return header,res
if __name__ == "__main__":
http_methode = HTTPMethod(sys.argv[1])
data = {
"UserName" : "monitor",
"Password" : "monitoring_shinken"
}
res, header = http_methode.post("/SessionService/Sessions/",data)
print(res,header)
res,header = http_methode.get("/Chassis/1/Thermal/")
print(res,header)
res, header = http_methode.delete("/SessionService/Sessions/")
print(res,header)
#print(http_methode.url)