#!/usr/bin/python3
#-*- coding: utf-8 -*-

import urllib.parse
import json
import base64
import requests
import logging
class Network():
    LOGIN_URL = 'http://192.168.211.101/portal/pws?t=li'
    BEAT_URL = 'http://192.168.211.101/portal/page/doHeartBeat.jsp'
    COMMON_HERADERS = {
    'Accept-Language': 'en-US',
    'Accept': 'text/html'
    }
    def __init__(self, username, password):
        b64Password = base64.b64encode(bytes(password,'utf8'))
        self.data = {'userName': username, 'userPwd': b64Password}

    def login(self):
        logging.info('login:%s'%(self.data))
        response = requests.post(Network.LOGIN_URL, data=self.data,
         headers=Network.COMMON_HERADERS, timeout=3)
        responseText = base64.b64decode(response.text + '==')
        responseJson = urllib.parse.unquote(responseText.decode('utf8'))
        jsonDict = json.loads(responseJson)
        heartBeatCyc = jsonDict.get('heartBeatCyc')
        if heartBeatCyc == None:
            raise BaseException(responseJson)
        logging.info('login seccuss: %s'%(responseJson))
        self.heartBeatCyc = int(heartBeatCyc)
        self.serialNo = jsonDict.get('serialNo')
        return self.heartBeatCyc

    def beat(self):
        response = requests.post(Network.BEAT_URL, data={'serialNo': self.serialNo},
         headers=Network.COMMON_HERADERS, timeout=3)
        if response.text.find('v_failedTimes') is -1:
            raise BaseException(response.text)

[((21, 8, 21, 44), 'logging.info', 'logging.info', ({(21, 21, 21, 43): "('login:%s' % self.data)"}, {}), "('login:%s' % self.data)", False, 'import logging\n'), ((22, 19, 23, 52), 'requests.post', 'requests.post', (), '', False, 'import requests\n'), ((24, 23, 24, 61), 'base64.b64decode', 'base64.b64decode', ({(24, 40, 24, 60): "response.text + '=='"}, {}), "(response.text + '==')", False, 'import base64\n'), ((26, 19, 26, 43), 'json.loads', 'json.loads', ({(26, 30, 26, 42): 'responseJson'}, {}), '(responseJson)', False, 'import json\n'), ((30, 8, 30, 56), 'logging.info', 'logging.info', ({(30, 21, 30, 55): "('login seccuss: %s' % responseJson)"}, {}), "('login seccuss: %s' % responseJson)", False, 'import logging\n'), ((36, 19, 37, 52), 'requests.post', 'requests.post', (), '', False, 'import requests\n')]