Commit 801fc43b authored by Norris Hua's avatar Norris Hua
Browse files

add comments

added some comments
parent 983f3a30
Loading
Loading
Loading
Loading
+14 −2
Original line number Diff line number Diff line
@@ -12,8 +12,10 @@ def printWithTimeStamp(s):
    ms=int(datetime.datetime.now().microsecond/1000)
    ms='%03d'%ms
    print ('['+time.strftime('%Y-%m-%d %H:%M:%S',time.localtime(time.time()))+'.'+ms+'] '+s)     
    #May cause errors when string s contains unicode characters
    
def newConfig():
#Set up new config using default settings
    username=input('Username: ')
    passwd=input('Password: ')
    f=open('config.json','w')
@@ -29,6 +31,7 @@ def readConfig():
        try:
            config=json.loads(f.readline())
            config and config['interval_retry_login'] and config['interval_check_status'] and config['username'] and config['password'] and config['interval_retry_connection'] and config['testUrl']
            #Check if the parameters all exist
        except KeyError as err:
            printWithTimeStamp('Parameter '+str(err)+' not found.')
            printWithTimeStamp('Configuration file is broken. Create a new one.')
@@ -48,6 +51,7 @@ def loadConfig():

def ifLoggedIn(test):
    if test.text.find(r'Central Authentication Service') == -1:
    #The login page of CAS will contain these words
        return True
    return False
  
@@ -78,8 +82,10 @@ def main():
            printWithTimeStamp('Parameters successfully acquired. wlanacip = '+wlanacip+', wlanuserip = '+wlanuserip)

            h={'Host': 'weblogin.sustc.edu.cn', 'Connection': 'keep-alive', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Upgrade-Insecure-Requests': '1' ,'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36', 'DNT': '1', 'Accept-Encoding': 'gzip, deflate, sdch', 'Accept-Language': 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4'}
            #Headers are acquired by capturing network traffic
        
            url='http://weblogin.sustc.edu.cn/cas/login?service=http%3A%2F%2Fenet.10000.gd.cn%3A10001%2Fsz%2Fsz112%2Findex.jsp%3Fwlanacip%3D'+wlanacip+'%26wlanuserip%3D'+wlanuserip
            #wlanacip & wlanuserip
        
            printWithTimeStamp('Start to get login information')

@@ -92,33 +98,39 @@ def main():
            lt=prelogin.text[start:end]
            end=lt.find(r'" />')
            lt=lt[0:end]
            #Get parameter "lt"
            
            start=prelogin.text.find(r'name="execution"')+24
            end=start+10
            execution=prelogin.text[start:end]
            end=execution.find(r'" />')
            execution=execution[0:end]
            #Get parameter "execution"
        
            start=prelogin.text.find(r'name="_eventId"')+23
            end=start+10
            _eventId=prelogin.text[start:end]
            end=_eventId.find(r'" />')
            _eventId=_eventId[0:end]
            #Get parameter "_eventld"
        
            end=prelogin.headers['Set-Cookie'].find(r'; Path=/cas/;')
            JSESSIONID_SUSTC= prelogin.headers['Set-Cookie'][11:end]
            #JSESSIONID stored in set-cookies
        
            printWithTimeStamp('Parameter JSESSIONID acquired. JSESSIONID = '+JSESSIONID_SUSTC)

            url='http://weblogin.sustc.edu.cn/cas/login;jsessionid='+JSESSIONID_SUSTC+'?service=http://enet.10000.gd.cn%3A10001%2Fsz%2Fsz112%2Findex.jsp%3Fwlanacip%3D'+wlanacip+'%26wlanuserip%3D'+wlanuserip
            username=config['username']
            passwd=config['password']
            #SUSTC CAS username (StudentID) and password
            data='username='+username+'&password='+passwd+'&lt='+lt+'&execution='+execution+'&_eventId='+_eventId+'&submit=LOGIN'

            h={'Host': 'weblogin.sustc.edu.cn', 'Connection': 'keep-alive', 'Cache-Control': 'max-age=0', 'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8', 'Origin': 'http://weblogin.sustc.edu.cn', 'Upgrade-Insecure-Requests': '1', 'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/49.0.2623.112 Safari/537.36', 'Content-Type': 'application/x-www-form-urlencoded', 'DNT': '1', 'Referer': url, 'Accept-Encoding': 'gzip, deflate', 'Accept-Language': 'en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4'}
        
            #Acquired by capturing traffic
            cookies=dict({'Cookie': 'JSESSIONID='+JSESSIONID_SUSTC})
            login=requests.Session()
            #Use session to maintain cookies
            printWithTimeStamp('Login as '+username)

            login.post(url,data=data,headers=h,cookies=cookies)