forked from parachvte/CourseSelection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
training.py
74 lines (63 loc) · 2.04 KB
/
training.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
# coding: UTF-8
import httplib
import urllib
#import getpass
import time
import OCR
host = "xk.fudan.edu.cn"
studentId = raw_input("studentID: ")
password = raw_input("password: ")
headers = {
"Connection": "keep-alive",
"Cache-Control": "max-age=0",
"Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8",
"Origin": "http://xk.fudan.edu.cn",
"User-Agent": "Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/28.0.1500.95 Safari/537.36",
"Content-Type": "application/x-www-form-urlencoded",
"Referer": "http://xk.fudan.edu.cn/xk/",
"Accept-Encoding": "gzip,deflate,sdch",
"Accept-Language": "zh-CN,zh;q=0.8",
}
def login():
conn = httplib.HTTPConnection(host)
conn.request("GET", "/xk")
res = conn.getresponse()
res.read()
conn.request("GET", "/xk/image.do")
res = conn.getresponse()
if res.getheader("Set-Cookie") is not None:
headers['Cookie'] = res.getheader("Set-Cookie").split(";")[0]
with open("captcha.jpg", "wb") as F:
F.write(res.read())
# OCR
OCRHdl = OCR.CaptchaHandler()
rand = OCRHdl.captcha_from_image("captcha.jpg")
# POST form
data = urllib.urlencode({"studentId": studentId,
"password": password,
"rand": rand,
"Submit2": r"提交"})
try:
time.sleep(0.2)
conn.request("POST", "/xk/loginServlet", data, headers)
res = conn.getresponse()
F = open("out.html", "w")
F.write(res.read())
F.close()
except Exception as e:
print 'Exception'
return False
conn.close()
result = res.reason == "Found"
if result:
OCRHdl.save_attribute_codes([i for i in rand])
return result
if __name__ == "__main__":
no = 0
while True:
no += 1
if login():
print 'Yes {}'.format(no)
else:
print 'No'
no = 0