【安全】校园论坛登录自动签到

为了练习python和理解http请求,在【测试校园网认证系统账号密码】后萌生了写个自动登录校园论坛签到脚本的念头,算是对上一篇的提高;

我校的论坛的基于discuz!X2.5,在获取手动登录和手动签到的请求包及URL的基础上,着手开始该python程序的编写;

有一点需要注意,而且至今仍然不是很明白!

在登录的时候request的数据中包含了headers,这点没问题,但签到的时候如果request中包含headers,就显示”未定义操作“!

机缘巧合地在网上看到类似的代码,发现在登录和签到的时候都没有request它的headers,我尝试地去掉签到里的header,居然签到成功了!兴奋!

问了下琛哥,他说可能是我写的header头没有完整,我确实没有把chrome-network中捕捉到的header全填进去,只是随意的填了个”Content-Type“!

服务器去匹配我的header发现少了很多东西,于是请求失败,这点可能解释的通,但为什么我在登录的时候header也只有”Content-Type“却可以呢?

看来依然没有一个肯定的答案!不懂啊!暂且记录下来吧 ╮(╯▽╰)╭ 说不定以后有机会遇到类似问题懂了再回来填上这个坑 ..

python3.3.2版本

  1 import urllib.parse,urllib.request,http.cookiejar,re,time
  2 
  3 class BitptRobot:
  4 
  5     def __init__(self, forumUrl, username, password):
  6         # 初始化论坛URL 用户名 密码
  7         self.forumUrl = forumUrl
  8         self.username = username
  9         self.password = password
 10         self.xq = 'kx'
 11         self.formhash = ''
 12         self.isLogon = False
 13         self.isSign = False
 14         self.jar = http.cookiejar.CookieJar()
 15 
 16         opener = urllib.request.build_opener(urllib.request.HTTPCookieProcessor(self.jar))
 17         urllib.request.install_opener(opener)
 18 
 19     def login(self):
 20         # 登陆
 21         url = self.forumUrl + "/member.php?mod=logging&action=login&loginsubmit=yes&infloat=yes&inajax=1"
 22         form = {
 23             'fastloginfield':'username',
 24             'username':self.username,
 25             'password':self.password,
 26             'quickforward':'yes',
 27             'questionid':'0',
 28             'handlekey':'ls',
 29             'answer':'',
 30             'cookietime':'2592000'
 31             }
 32         postData = urllib.parse.urlencode(form).encode(encoding = 'UTF8')
 33         header = {
 34             'Content-Type':'application/x-www-form-urlencoded'
 35             }
 36         req = urllib.request.Request(
 37             url = url,
 38             data = postData,
 39             headers = header
 40             )
 41         content = urllib.request.urlopen(req).read().decode('UTF8')
 42         if self.username in content:
 43             self.isLogon = True
 44             print('login success!')
 45             
 46         else:
 47             print('login faild!')
 48         return
 49 
 50     def initFormhash(self):
 51         # get formhash
 52         content = urllib.request.urlopen(self.forumUrl + '/plugin.php?id=dsu_paulsign:sign').read().decode('UTF8')
 53         rows = re.findall(r'<input type="hidden" name="formhash" value="(.*?)">', content)
 54         if len(rows) != 0:
 55             self.formhash = rows[0]
 56             print('formhash is: ' + self.formhash)
 57         else:
 58             print('None formhash')
 59             print('Signed before!')
 60 
 61     def sign(self):
 62         # 签到
 63         if self.isSign:
 64             return
 65         if self.isLogon and self.xq:
 66             self.initFormhash()
 67             url = self.forumUrl + '/plugin.php?id=dsu_paulsign:sign&operation=qiandao&infloat=1&inajax=1'
 68             form = {
 69                 'fastreply':'1',
 70                 'formhash':self.formhash,
 71                 'qdmode':'3',
 72                 'qdxq':self.xq,
 73                 'todaysay':''
 74                 }
 75             postData = urllib.parse.urlencode(form).encode(encoding = 'UTF8')
 76             req = urllib.request.Request(url,postData)
 77             content = urllib.request.urlopen(req).read().decode('UTF8')
 78             # print(content)
 79             if u'签到成功' in content:
 80                 self.isSign = True
 81                 print('sign success!')
 82                 return
 83         print('sign faild!')
 84 
 85 if __name__ == '__main__':
 86     robot = BitptRobot('http://bitpt.cn/bbs','RAUL','wmw528392')
 87     robot.login()
 88     time_now = time.strftime('%H-%M',time.localtime(time.time()))
 89     # print(time_now)
 90     # print(time.strftime('%H-%M',time.localtime(time.time())))
 91     if time_now >= '23-58':
 92         while True:
 93             # print (robot.isSign)
 94             if robot.isSign == False:
 95                 robot.sign()
 96                 time.sleep(0.1)
 97             else:
 98                 break
 99     
100     #----------------------------------------------------------------
101     #robot = BitptRobot('http://bitpt.cn/bbs','username','password')
102     #robot.login()
103     #robot.sign()
104     #----------------------------------------------------------------

更新一版,果然没经过测试的东西不靠谱,本来想抢个新年第一签,没抢到 .. 算了,估计是象征着我要淡出极速了 .. 也好 ~ 下次测试成功再发个最终版

原文地址:https://www.cnblogs.com/raul-ac/p/3492984.html