Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- #
- #
- try:
- import json
- import re
- import sys
- import requests
- with open("config.json") as stream:
- CONFIG = (json.load(stream))
- except Exception as e:
- print (e)
- sys.exit(1)
- #
- #
- USER = CONFIG["USER"]
- PASSWORD = CONFIG["PASSWORD"]
- BASEURL = CONFIG["BASEURL"]
- SESSION_COOKIE = ''
- CSRFTOKEN = ''
- #
- #
- def login():
- try:
- #Headers for each request. Authorisation is going here
- headers = {'accept': 'application/json', 'Content-Type': 'application/json;charset=UTF-8'}
- payload = json.dumps({'username': USER, 'password': PASSWORD, 'remember': False, 'strict': True})
- api_request = '/login'
- url = '%s%s' % (BASEURL, api_request)
- #we use ip adress as host, so we have notification about ssl insecure, next line disable this warning
- requests.packages.urllib3.disable_warnings()
- response = requests.request("POST", url, headers=headers, data=payload, verify=False)
- global SESSION_COOKIE
- global CSRFTOKEN
- SESSION_COOKIE = response.cookies["unifises"]
- CSRFTOKEN = response.cookies["csrf_token"]
- rval = response.text
- #print(rval)
- except Exception as e:
- print (e)
- return ("FAIL")
- def parse_path(x2):
- regex = re.compile('\[\"(\w+)\"\]\[(\d+)\]')
- match = re.match(x2)
- return match.groups()
- def get_basic_devices_info(x1, x2):
- # x2 = ['data'][0]
- try:
- login()
- headers = {'accept': 'application/json', 'Content-Type': 'application/json;charset=UTF-8', 'X-Csrf-Token': CSRFTOKEN}
- api_request = x1
- url = '%s%s' % (BASEURL, api_request)
- jar = requests.cookies.RequestsCookieJar()
- jar.set('unifises', SESSION_COOKIE)
- jar.set('csrf_token', CSRFTOKEN)
- #we use ip adress as host, so we have notification about ssl insecure, next line disable this warning
- requests.packages.urllib3.disable_warnings()
- response = requests.request("GET", url, headers=headers, cookies=jar, verify=False)
- rval = response.json()
- key, index = parse_path(x2)
- var = rval[key][index] #rval['data'][0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement