# -*- coding: utf-8 -*-
import pycurl
import re
try:
    from cStringIO import StringIO
except:
    from StringIO import StringIO

header = [
    'Accept:text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8',
    'Accept-Encoding:gzip, deflate, sdch',
    'Accept-Language:en-US,en;q=0.8,zh-CN;q=0.6,zh;q=0.4',
    'Cache-Control:max-age=0',
    'Connection:keep-alive',
    'Host:uems.sysu.edu.cn',
    'User-Agent:Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/39.0.2171.99 Safari/537.36'
    ]

class Jwxt:
    def __init__(self):
        self.cookiePattern = re.compile(r'(?<=Set-Cookie:\ )(.+?)(?=;)')
        self.cookie = None
        self.header = header

    def getData(self, url, headerCallback, callback):
        connect = pycurl.Curl()
        connect.setopt(connect.URL, url)
        connect.setopt(connect.HTTPHEADER, self.header)
        connect.setopt(connect.HEADERFUNCTION, headerCallback)
        connect.setopt(connect.WRITEFUNCTION, callback)
        connect.perform()
        connect.close()

    def addHeader(self, newLine):
        self.header.append(newLine)

    def passHeader(self, headerLine):
        pass

    def passWrite(self, data):
        pass

    def cookieHeaderFunction(self, headerLine):
        field = headerLine.split(':')[0]
        if field == 'Set-Cookie':
            cookie = self.cookiePattern.search(headerLine).group()
            self.cookie = cookie
            self.addHeader('Cookie: ' + self.cookie)

    def getCookie(self):
        self.getData('http://uems.sysu.edu.cn/jwxt/',
            self.cookieHeaderFunction, self.passWrite)

    

jwxt = Jwxt()
jwxt.getCookie()


@@ -31,16 +31,30 @@
  pycurl%0A
+import urllib%0A
 import r
@@ -550,16 +550,77 @@
 /537.36'
+,%0A    'Content-Type:multipart/form-data',%0A    'render:unieap'
 %0A    %5D%0A%0A
@@ -783,16 +783,183 @@
 = header
+%0A        self.urls = %7B%0A            'login': 'http://uems.sysu.edu.cn/jwxt/j_unieap_security_check.do',%0A            'cookie': 'http://uems.sysu.edu.cn/jwxt/',%0A        %7D
 %0A%0A    de
@@ -1317,68 +1317,600 @@
 def 
-addHeader(self, newLine):%0A        self.header.append(newLine
+postData(self, url, data, headerCallback, callback):%0A        postData = urllib.urlencode(data)%0A        connect = pycurl.Curl()%0A        connect.setopt(connect.URL, url)%0A        connect.setopt(connect.FOLLOWLOCATION, True)%0A        connect.setopt(connect.POST, True)%0A        connect.setopt(connect.POSTFIELDS, postData)%0A        connect.setopt(connect.COOKIE, self.cookie)%0A        connect.setopt(connect.HEADERFUNCTION, headerCallback)%0A        connect.setopt(connect.WRITEFUNCTION, callback)%0A        connect.setopt(connect.HTTPHEADER, self.header)%0A        connect.perform()%0A        connect.close(
 )%0A%0A 
@@ -2244,48 +2244,85 @@
     
-self.addHeader('Cookie: ' + self.cookie)
+print self.cookie%0A%0A    def loginWriteFunction(self, data):%0A        print data
 %0A%0A  
@@ -2369,39 +2369,27 @@
 ata(
-'http://uems.sysu.edu.cn/jwxt/'
+self.urls%5B'cookie'%5D
 ,%0A  
@@ -2450,38 +2450,337 @@
     
-%0A%0Ajwxt = Jwxt()%0Ajwxt.getCookie(
+def login(self, username, password):%0A        if self.cookie is None:%0A            self.getCookie()%0A        self.postData(self.urls%5B'login'%5D, %7B%0A            'j_username': username,%0A            'j_password': password,%0A            %7D, self.passHeader, self.loginWriteFunction)%0A    %0A%0Ajwxt = Jwxt()%0Ajwxt.login('12330071', '33519000091533'
 )%0A%0A

Add login function