python模拟登录某高校教务网

#encoding=utf8
import urllib  
import urllib2  
import cookielib  
import hashlib


def get_cap():
    url = 'http://223.2.96.8:8090/sys/ValidateCode.aspx';
    http = urllib2.Request(url)
    html = opener.open(http)
    f = open('/root/Desktop/nanlin_cap.png','wb')
    f.write(html.read())
    f.flush()
    f.close()
    
def input_cap():
    get_cap()
    cap = raw_input('请输入验证码:')
    return cap
    
def d_md5(username,password):
    m1 = hashlib.md5()
    m1.update(password)
    m1_str = m1.hexdigest()[0:30].upper()
    m2_str = username+m1_str+'10298'
    m2 = hashlib.md5(m2_str)
    m2_str = m2.hexdigest()[0:30].upper()
    end_str = m2_str
    return end_str
# 309701A43F32D879166C4BE935793F
    
def g_md5(cap):
    m1 = hashlib.md5()
    cap = cap.upper()
    m1.update(cap)
    m1_str = m1.hexdigest()[0:30].upper()+'10298'
    m2 = hashlib.md5(m1_str)
    m2_str = m2.hexdigest()[0:30].upper()
    return m2_str
    
if __name__=='__main__':
    #登录的主页面  
    hosturl = 'http://223.2.96.8:8090/'
    #post数据接收和处理的页面(我们要向这个页面发送我们构造的Post数据)  
    posturl = 'http://223.2.96.8:8090/_data/index_LOGIN.aspx'  
    url = 'http://223.2.96.8:8090/SYS/Main_banner.aspx'  
#     http://223.2.96.8:8090/_data/index_LOGIN.aspx
    #设置一个cookie处理器,它负责从服务器下载cookie到本地,并且在发送请求时带上本地的cookie  
    cj = cookielib.MozillaCookieJar()  
    cookie_support = urllib2.HTTPCookieProcessor(cj)  
    opener = urllib2.build_opener(cookie_support, urllib2.HTTPHandler)  
    urllib2.install_opener(opener)  
          
    #打开登录主页面(他的目的是从页面下载cookie,这样我们在再送post数据时就有cookie了,否则发送不成功)  
    h = urllib2.urlopen(hosturl)  
          
    #构造header,一般header至少要包含一下两项。这两项是从抓到的包里分析得出的。  
    headers = {'User-Agent' : 'Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Firefox/38.0 Iceweasel/38.3.0',  
                   'Referer' : 'http://223.2.96.8:8090/_data/index_LOGIN.aspx',
                   'Host' : '223.2.96.8:8090',
                   'Accept-Language' : 'en-US,en;q=0.5',
                   'Accept': 'text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8',
                   'Accept-Language' : 'en-US,en;q=0.5',
                   'Accept-Encoding' : 'gzip, deflate',
                   'Connection' : 'keep-alive',
                   'Cookie' : 'ASP.NET_SessionId=w0puh345sjrptlnadwux5iez',
                   'Content-Type' : 'application/x-www-form-urlencoded',
    }
    #构造Post数据,他也是从抓大的包里分析得出的。  
    username = '130801327'
    password = '456258'
    ds = d_md5(username,password)
    print(ds)
    capp = input_cap()
    fg = g_md5(capp)
    postData = {  
                    '__VIEWSTATE':'dDw4ODEwMTkyNTY7Oz6/35PpjccX0HYe5QRB9AaxnHHkrA==',
                    'pcInfo':'Mozilla/5.0+(X11;+Linux+x86_64;+rv:38.0)+Gecko/20100101+Firefox/38.0+Iceweasel/38.3.0Linux+x86_645.0+(X11)+SN:NULL"',
                    'typeName' : '学生',
                    'dsdsdsdsdxcxdfgfg':ds,
                    'fgfggfdgtyuuyyuuckjg':fg,
                    'Sel_Type' : 'STU',
                    'txt_asmcdefsddsd':username,
                    'txt_pewerwedsdfsdff' : '',
                    'txt_sdertfgsadscxcadsads' : '',
                    'sbtState' : ''
                    }  
          
    #需要给Post数据编码  
    postData = urllib.urlencode(postData)  
          
    #通过urllib2提供的request方法来向指定Url发送我们构造的数据,并完成登录过程  
    request = urllib2.Request(posturl, postData, headers)  
    response = urllib2.urlopen(request)  
    text = response.read().decode('gbk').encode('utf8');  
    print(text)
    
#     print('******************************************************************')
#     
#     content = urllib2.urlopen('http://223.2.96.8:8090/SYS/Main_banner.aspx')
#     content = content.read().decode('gbk').encode('utf8');
#     print(content)

转载于:https://my.oschina.net/passer007/blog/605881

你可能感兴趣的:(python模拟登录某高校教务网)