Advertisement
nocwat

Wholeton Python Authenticate Client

May 4th, 2022 (edited)
967
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.75 KB | None | 0 0
  1. #!/usr/bin/python
  2. # -*- coding: utf-8 -*-
  3.  
  4. import sys
  5. import socket
  6. from datetime import datetime
  7. from uuid import getnode
  8. import urllib
  9. try:
  10.     import urllib2
  11. except Exception:
  12.     from urllib import request as urllib2
  13. try:
  14.     import Cookie as cookies
  15. except Exception:
  16.     from http import cookies
  17. import websocket
  18. import json
  19.  
  20. wholeton_host = '192.168.1.254'
  21. wholeton_user = 'test'
  22. wholeton_pass = '123456'
  23. wholeton_ip = ''
  24. wholeton_mac = ''
  25. update_secs = 28800
  26.  
  27. def url_encode(obj):
  28.     try:
  29.         return urllib.urlencode(obj)
  30.     except Exception:
  31.         return urllib.parse.urlencode(obj)
  32.  
  33. def get_ip():
  34.     s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
  35.     try:
  36.         s.connect(('10.255.255.255', 1))
  37.         IP = s.getsockname()[0]
  38.     except Exception:
  39.         IP = '127.0.0.1'
  40.     finally:
  41.         s.close()
  42.     return IP
  43.  
  44. def get_mac():
  45.     return ':'.join(("%012x" % getnode())[i:i+2] for i in range(0, 12, 2))
  46.  
  47. if not wholeton_ip:
  48.     wholeton_ip = get_ip()
  49.  
  50. if not wholeton_mac:
  51.     wholeton_mac = get_mac()
  52.  
  53. uri_keys = { 'id' : 0, 'url' : 'mail.126.com', 'user' : wholeton_ip, 'mac' : wholeton_mac }
  54. uri_data = url_encode(uri_keys).replace('%3A', ':')
  55.  
  56. auth_data = url_encode({ 'param[UserName]' : wholeton_user, 'param[UserPswd]' : wholeton_pass, 'uri' : uri_data, 'force' : 0 })
  57. # convert for python 3
  58. if sys.version_info[0] == 3:
  59.     auth_data = auth_data.encode('ascii')
  60.  
  61. ws = None
  62. loop = True
  63.  
  64. try:
  65.     while loop:
  66.         resp = urllib2.urlopen('http://' + wholeton_host + '/user-login-auth?' + uri_data, timeout = 5, data = auth_data)
  67.  
  68.         # get session cookie
  69.         cookie = cookies.SimpleCookie()
  70.         cookie.load(resp.info()['Set-Cookie'])
  71.  
  72.         resp_data = resp.read()
  73.         if resp_data:
  74.             print('Login response:')
  75.             print(resp_data)
  76.  
  77.         ws = websocket.WebSocket()
  78.         ws.connect('ws://' + wholeton_host + '/go-ws/user-auth', cookie = 'fms_session=' + cookie.get('fms_session').value, origin = 'http://' + wholeton_host)
  79.  
  80.         dt_start = datetime.now()
  81.         while ws:
  82.             try:
  83.                 ws_data = ws.recv()
  84.             except KeyboardInterrupt:
  85.                 loop = False
  86.                 break
  87.             except:
  88.                 break
  89.  
  90.             if ws_data:
  91.                 dt_now = datetime.now()
  92.                 if (dt_now - dt_start).seconds >= update_secs:
  93.                     break
  94.  
  95.                 print(dt_now)
  96.                 print(ws_data)
  97.  
  98.                 ws_obj = json.loads(ws_data)
  99.                 if ws_obj and ws_obj["type"] == "logged-out":
  100.                     break
  101.  
  102.         if ws:
  103.             ws.close()
  104.         ws = None
  105. except KeyboardInterrupt:
  106.     pass
  107.  
  108. if ws:
  109.     ws.close()
  110.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement