-
Notifications
You must be signed in to change notification settings - Fork 2
/
sustc-cas-login.py
151 lines (123 loc) · 4.09 KB
/
sustc-cas-login.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
#!/usr/bin/env python
# -*- coding: utf-8 -*-
import sys
import time
import urllib
import urllib2
import cookielib
import socket
import re
__TIMEOUT__ = 2
# 10 min
__PERIOD__ = 10 * 60
class NoRedirectHandler(urllib2.HTTPErrorProcessor):
def http_response(self, request, response):
return response
https_response = http_response
def init_env():
# cookie processor
cookie = cookielib.CookieJar()
#httpHandler = urllib2.HTTPHandler(debuglevel = 1)
#httpsHandler = urllib2.HTTPSHandler(debuglevel = 1)
cookieprocessor = urllib2.HTTPCookieProcessor(cookie)
opener = urllib2.build_opener(cookieprocessor)
#opener = urllib2.build_opener(cookieprocessor, httpHandler, httpsHandler)
urllib2.install_opener(opener)
def dologin(username, password):
login(username, password)
def login(username, password):
opener = urllib2.build_opener(NoRedirectHandler)
try:
req = urllib2.Request('http://baidu.com')
response = opener.open(req, timeout = __TIMEOUT__)
location = response.info().getheader('Location')
except:
return
if location and "http://enet.10000.gd.cn" in location:
date = time.strftime('%Y-%m-%d %H:%M:%S')
print '[%s] Logging...' % date,
sys.stdout.flush()
success = False
content = None
try:
response = urllib2.urlopen(location, timeout = __TIMEOUT__)
content = response.read()
#except socket.timeout, e:
# print "There was an error: %r" % e
#except urllib2.URLError, e:
# print "There was an error: %r" % e
except:
pass
if content:
patt_action = re.compile(r'<form .*?action="(.*?)"', re.M)
patt_lt = re.compile(r'<input type="hidden" name="lt" .*?value="(.*?)"', re.M)
patt_exec = re.compile(r'<input type="hidden" name="execution" .*?value="(.*?)"', re.M)
mat_action = patt_action.findall(content)
mat_lt = patt_lt.findall(content)
mat_exec = patt_exec.findall(content)
if mat_action and mat_lt and mat_exec:
action = mat_action[0]
lt = mat_lt[0]
execution = mat_exec[0]
data = {
'username': username,
'password': password,
'lt': lt,
'execution': execution,
'_eventId': 'submit',
'submit': 'LOGIN'
}
# login
url = 'http://weblogin.sustc.edu.cn' + action
data = urllib.urlencode(data)
content = None
timeout = False
try:
response = urllib2.urlopen(url, data = data, timeout = __TIMEOUT__)
content = response.read()
except socket.timeout, e:
timeout = True
except urllib2.URLError, e:
timeout = True
except:
pass
if timeout:
print 'Timeout...',
try:
response = urllib2.urlopen(url, data = data, timeout = __TIMEOUT__)
content = response.read()
except:
pass
if content and '<h2>success' in content:
success = True
if success:
print "Success"
else:
print "Failed"
sys.stdout.flush()
def usage():
print """Usage:
./sustc-cas-login.py [--loop] [username] [password]
"""
def main(argv):
if len(argv) < 2:
usage()
return
loop = False
i = 0
if argv[0] == '--loop':
loop = True
i += 1
# check args again
if loop and len(argv) < 3:
usage()
return
username = argv[i]
password = argv[i + 1]
init_env()
dologin(username, password)
while loop:
time.sleep(__PERIOD__)
dologin(username, password)
if __name__ == '__main__':
main(sys.argv[1:])