嘉兴学院公寓的PYTHON拨号上网方法

背景介绍:ACTIVEX+IE的网页认证方式。目前普遍无解决良方
在网络上看到用脚本语言模拟浏览器的文章,受到启发。
本程序遵从GPL!    嘿嘿,通宵了2天终于写出来了 ,虽然很不完美,但是还能将就着用吧。
本人的PYTHON总共才看了1天,所以按照C的感觉写的,见笑了。
有问题清联系:风再起时 QQ:505530025 信息工程学院的小小鸟⑩一只
文件名字:jxxyhh.py
#!/usr/bin/env python
#Filename: jxxyhh.py
import string, getopt, sys, httplib, urllib, time
def main():
    #定义一堆头文件>_>>",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用的
    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()