Advertisement
Guest User

Untitled

a guest
Mar 22nd, 2019
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.99 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. #
  3. #
  4. try:
  5.     import json
  6.     import re
  7.  
  8.     import sys
  9.     import requests
  10.     with open("config.json") as stream:
  11.         CONFIG = (json.load(stream))
  12. except Exception as e:
  13.     print (e)
  14.     sys.exit(1)
  15.  
  16. #
  17. #
  18. USER = CONFIG["USER"]
  19. PASSWORD = CONFIG["PASSWORD"]
  20. BASEURL = CONFIG["BASEURL"]
  21. SESSION_COOKIE = ''
  22. CSRFTOKEN = ''
  23. #
  24. #
  25. def login():
  26.     try:
  27.         #Headers for each request. Authorisation is going here
  28.         headers = {'accept': 'application/json', 'Content-Type': 'application/json;charset=UTF-8'}
  29.         payload = json.dumps({'username': USER, 'password': PASSWORD, 'remember': False, 'strict': True})
  30.         api_request = '/login'
  31.         url = '%s%s' % (BASEURL, api_request)
  32.         #we use ip adress as host, so we have notification about ssl insecure, next line disable this warning
  33.         requests.packages.urllib3.disable_warnings()
  34.         response = requests.request("POST", url, headers=headers, data=payload, verify=False)
  35.         global SESSION_COOKIE
  36.         global CSRFTOKEN
  37.         SESSION_COOKIE = response.cookies["unifises"]
  38.         CSRFTOKEN = response.cookies["csrf_token"]
  39.         rval = response.text
  40.         #print(rval)
  41.     except Exception as e:
  42.         print (e)
  43.         return ("FAIL")
  44.  
  45.  
  46. def parse_path(x2):
  47.     regex = re.compile('\[\"(\w+)\"\]\[(\d+)\]')
  48.     match = re.match(x2)
  49.     return match.groups()
  50.  
  51. def get_basic_devices_info(x1, x2):
  52.     # x2 = ['data'][0]
  53.     try:
  54.         login()
  55.         headers = {'accept': 'application/json', 'Content-Type': 'application/json;charset=UTF-8', 'X-Csrf-Token': CSRFTOKEN}
  56.         api_request = x1
  57.         url = '%s%s' % (BASEURL, api_request)
  58.         jar = requests.cookies.RequestsCookieJar()
  59.         jar.set('unifises', SESSION_COOKIE)
  60.         jar.set('csrf_token', CSRFTOKEN)
  61.         #we use ip adress as host, so we have notification about ssl insecure, next line disable this warning
  62.         requests.packages.urllib3.disable_warnings()
  63.         response = requests.request("GET", url, headers=headers, cookies=jar, verify=False)
  64.         rval = response.json()
  65.         key, index = parse_path(x2)
  66.         var = rval[key][index] #rval['data'][0]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement