嘉兴学院公寓的PYTHON拨号上网方法
純白
|
1#
純白 发表于 2007-08-18 19:21
嘉兴学院公寓的PYTHON拨号上网方法
背景介绍:ACTIVEX+IE的网页认证方式。目前普遍无解决良方
在网络上看到用脚本语言模拟浏览器的文章,受到启发。 本程序遵从GPL! 嘿嘿,通宵了2天终于写出来了 ,虽然很不完美,但是还能将就着用吧。 本人的PYTHON总共才看了1天,所以按照C的感觉写的,见笑了。 有问题清联系:黄欢 QQ:505530025 由于对PYTHON的函数结构不熟悉,所以只写到这个程度. 文件名字:jxxyhh.py #!/usr/bin/env python #Filename: jxxyhh.py import string, getopt, sys, httplib, urllib, time def main(): #定义一堆头文件>_<! HOSTNAME = '10.254.0.9' ip='220.189.67.3' username='2004090600010' password='111111' portallogindata='IwMDQwOTA2MDAwMTA1ITExMTExNSM2NTAw==' #开始第一轮连接,不带JSESSIONID的,目的是从服务器获得一个用于后续的连接。 headers = {'Accept':'text/html', 'Accept-Language':'zh-cn','Accept-Encoding': 'gzip, deflate','User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)','Host': HOSTNAME ,'Connection':' Keep-Alive', 'Cookie':'cookieEnableTest=cookieEnable; isPNP=0; needActiveX=1;portallogindata='+portallogindata } conn = httplib.HTTPConnection(HOSTNAME) conn.request('GET', '/', None, headers) response = conn.getresponse() s = response.read() print s cookie = response.getheader("Set-Cookie") print "=====Cookie1====", cookie #就是这个cookie a=cookie[207:221] #a是JSESSIONID,笨人用笨办法了,嘿嘿。 print a #准备第二次的连接,用于POST自己的挨劈地址=_=!这里调了好久, headers = {'Accept': ' image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*','Referer': 'http://10.254.0.9/', 'Content-Type': 'application/x-www-form-urlencoded','Accept-Language': 'zh-cn','Accept-Encoding': 'gzip, deflate','User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)','Host': HOSTNAME ,'Connection':' Keep-Alive','Cache-Control':' no-cache', 'Cookie': 'cookieEnableTest=cookieEnable; isPNP=0; needActiveX=1; portallogindata='+portallogindata+',JSESSIONID='+a, } #这一步我这里不能少, 需要发送自己的IP, 这里的IP在后面会和刚才session联系起来, 以后keep-alive的参数要用到 #登录http://10.254.0.9/webLogin.jsp conn = httplib.HTTPConnection(HOSTNAME) conn.request('POST', '/webLogin.jsp', "LocalIP=" + ip, headers) response = conn.getresponse() s = response.read() print s #刚开始时,老是NOCOOKIE,头大-_-! #第三步, 这是最关键的一步, 就是发送认证信息的请求 #third https://10.254.0.9:443/secu/webLogin.jsp,开始时候定义下SSL发送头 params = urllib.urlencode({'connectname': '', 'connecttype': -1, 'consumeright': 0, 'separatecard': 0, 'localip': ip, 'IsIndex': 0, 'username': username, 'password': password, 'cookiedate': 0}) print '===params===',params conn.request('POST', '/secu/webLogin.jsp', params, headers) #在WINXP下这里老出错!在LINUX下没有问题。应该是在WIN下的包安装不完全的原因。 response = conn.getresponse() s = response.read() start = string.find(s, HOSTNAME) end = string.find(s, "')", start) s = s[start:end] URL = s cookie3 = response.getheader("Set-Cookie") print URL print "cookie3----->>>",cookie3 #第四步, 获取注册信息,这是keep-alive的关键步骤,这里的请求地址是上个response返回的. #four get regist parameters conn = httplib.HTTPConnection(HOSTNAME) headers = {'Accept': ' image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */*','Accept-Language': 'zh-cn','Accept-Encoding': 'gzip, deflate','User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)','Host': HOSTNAME ,'Connection':' Keep-Alive', 'Cookie': 'cookieEnableTest=cookieEnable; isPNP=0; needActiveX=1; portallogindata='+portallogindata+',JSESSIONID='+a, } start = string.find(URL, "/") end = string.find(URL, "?", start) URL2 = URL[start:end] params = URL[end+1:] print URL2, print headers print "=====params====", params idnum=params[13:26] print "==numkey==",idnum conn.request('GET', URL2, params, headers) response = conn.getresponse() s = response.read() print "主要注册信息网页" print s #是服务器应答的大括号你的东西。 #loginstr = s[start:end] print "新截取的信息" ac=s[2697:3090] print ac #loginstr = s[start:end] loginstr = ac loginstr = string.replace(loginstr, "\r", "") #string.replace(string1,a,b) 把把STRING1字符串里面的A,全部替换成B loginstr = string.replace(loginstr, "\n", "") loginstr = string.replace(loginstr, " ", "") loginstr = string.replace(loginstr, "'", "") print loginstr L = string.split(loginstr, ',') print '=====L====',L user = L[0] isnno = 1204 loginkey = L[6] ip2 = L[7] #是服务器应答的大括号你的东西。 #loginstr = s[start:end] #的格式应该类似于 2004090600010,.. 1,..0,..0,..2,..1000,....mGyL0TosmUBo4GyKIvA2-RBhVr4vznuxkQA9,..1 #第五步, 这时已经获取了注册信息,但是还不能keep-alive, 这一步是请求一个新的sessionID, 这个ID才是keep-alive用的 #这个是网络上铁通用户的说法,自己抓包也是这样,可是我一直用初始的JSSESSIONID也没问题,成功了7小时. path='/ClientProcess.jsp?MsgType=2&UserName=%s&LocalIP=%s&ConnectType=1&ConsumeRight=0&SeparateCard=0&ISNNO=1024&LoginKey=%s&isPNP=0&httpIP=%s HTTP/1.0' %(username,ip,loginkey,ip) print '===path===',path conn = httplib.HTTPConnection(HOSTNAME) headers = {'Accept': 'text/html', 'User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)','Cookie':cookie} print headers conn.request('GET', path, None, headers) #这里新的JSESSIONID没获取成功!*_*! #重要参数列表:loginkey:kl-cookie:还有一个数字ID(?貌似不重的。):大致就这些了。^_^ print '====user====', user print "=====loginkey====",loginkey print "===idnum====",idnum #最后几步了。 headers = {'Accept': 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */* ', 'Accept-Language': 'zh-cn','Accept-Encoding': 'gzip, deflate','User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)', 'Host': HOSTNAME, 'Connection': 'Keep-Alive', 'Cookie':'cookieEnableTest=cookieEnable;isPNP=0;needActiveX=1;portallogindata='+portallogindata+';JSESSIONID='+cookie } path = '/myportal/myindex.jsp?key='+idnum+' HTTP/1.0' print path conn = httplib.HTTPConnection(HOSTNAME) print headers conn.request('GET', path, None, headers) #要用的东西。。。。。。 pathx = '/ClientProcess.jsp?MsgType=1&ISNNO=1000&LocalIP=%s&LoginKey=%s&isPNP=0&httpIP=%s HTTP/1.0' %(ip,loginkey,ip) print pathx headersx = {'Accept': 'image/gif, image/x-xbitmap, image/jpeg, image/pjpeg, application/x-shockwave-flash, */* ', 'Accept-Language': 'zh-cn','Accept-Encoding': 'gzip, deflate','User-Agent': 'Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)', 'Host': HOSTNAME, 'Connection': 'Keep-Alive','Cookie ': ' JSESSIONID= '+cookie } print headersx conn = httplib.HTTPConnection(HOSTNAME) conn.request('GET', pathx, None, headersx) #每5分钟保持连接的调用函数 def kl(HOSTNAME,pathx,headersx): conn = httplib.HTTPConnection(HOSTNAME) conn.request('GET', pathx, None, headersx) for i in range(1,120): time.sleep(300.0) kl(HOSTNAME,pathx,headersx) else: print "Too long ! if you need ,you can retry!" main() |