学校的dr.com只能同时登陆一次,且不能”被挤下去“,所以写了个这样的东西。
原理很简单,就是登陆网页版”自助服务系统“,然后停开机一下就可以了。
我主要是为了练习python的填表操作,幸运的是这个”自助服务系统”没有图形验证码,只有一个文字的随机码,用正则表达式很容易就提取出来了。
还遇到的一个问题是cookie的问题,昨天弄了一下,用chrome的调试功能截取了请求头和回复头,照搬上去的,昨天好好的,今天发现不行,原来每次回话的时候都会重新set-cookies的。
另外,不知是什么原因,直接“停机”然后再“开机”,总有点问题,第二个操作只能进行,所以就采用了再次登录这种笨的解决办法。
没有什么错误处理,只有命令行界面,等下看看要不要写个图形界面的。
上代码:
#!/usr/bin/env python import httplib,urllib import re import hashlib def login(account,password): global headers1 conn = httplib.HTTPConnection("internet.ccsu.cn") headers1 ={ "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Encoding":"gzip,deflate,sdch", "Accept-Language":"zh-CN,zh;q=0.8", "Connection":"keep-alive", "DNT":"1", "Host":"internet.ccsu.cn", "User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36" } conn.request("GET","","",headers1) resp = conn.getresponse() dat1 = resp.read() cookies = resp.getheader("Set-Cookie") headers2 ={ "Accept":"text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8", "Accept-Encoding":"gzip,deflate,sdch", "Accept-Language":"zh-CN,zh;q=0.8", "Cache-Control":"max-age=0", "Connection":"keep-alive", #"Content-Length":str(len(params)), "Content-Type":"application/x-www-form-urlencoded", "Cookie":cookies, "DNT":"1", "Host":"internet.ccsu.cn", "Origin":"http://internet.ccsu.cn", "Referer":"http://internet.ccsu.cn/", "User-Agent":"Mozilla/5.0 (Windows NT 6.1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.94 Safari/537.36" } loginRandom = re.findall('''loginRandom = "(.+)"''',dat1)[0] password = hashlib.md5(password + loginRandom).hexdigest() params = urllib.urlencode({"account":account,"password":password}) conn.request("POST","/LoginAction.action",params,headers2) resp = conn.getresponse() dat2 = resp.read() #print "Set-Cookie:%s"%(cookies) #print "*"*80 if len(re.findall("(nav_business)",dat2))>0: return conn,headers2 else: conn.close() return 0,0 def switch(conn,headers,mode): params = urllib.urlencode({"opertype":mode,"Remark":"N/A","Summit":"submit"}); conn.request("POST","/SwitchAction",params,headers) dat3 = conn.getresponse().read() #print re.findall('''id="texttip"\D+<p>(.+)</p>''',dat3)[0] conn.close() def main(): print "*Force logout the dr.com account*" account = raw_input("Account:") password = raw_input("Password:") conn,headers=login(account,password) if conn == 0: print "Account or password error." else: print "Processing..." switch(conn,headers,2)#disable conn,headers=login(account,password) switch(conn,headers,1)#enable print "Done!" raw_input("Press any key to quit") if __name__ == "__main__": main()