示例#1
0
def chooseLesson(lesson):
    conn = httplib.HTTPConnection(host)

    # get captcha
    conn.request("GET", "/xk/input.jsp", None, headers)
    res = conn.getresponse()
    parser = MyHTMLParser()
    parser.feed(res.read())
    token = parser.get_token()
    if token is None:
        print "have bugs in my programme"
        return False

    conn.request("GET", "/xk/image.do?token={}".format(token), None, headers)
    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
    data = urllib.urlencode({"token": token,
                             "selectionId": lesson,
                             "xklb": "ss",
                             "rand": rand})
    try:
        time.sleep(3)
        conn.request("POST", "/xk/doSelectServlet", data, headers)
        res = conn.getresponse()
        F = open("out.html", "w")
        F.write(res.read())
        F.close()
        print u"返回结果写在out.html中,请查看"
    except Exception as e:
        print u"请求失败"
        return False

    # check if lesson is selected
    conn.request("GET", "/xk/courseTableServlet", None, headers)
    res = conn.getresponse()
    parser.set_lesson(lesson)
    parser.feed(res.read())
    conn.close()

    return parser.has_lesson()
示例#2
0
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