1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
""" 平安行动自动打卡
Created on 2020-04-13 20:20 @author: ZhangJiawei & Monst.x """
import requests import lxml.html import re import json import random import time import smtplib import traceback
headers = { "Accept": "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8,application/signed-exchange;v=b3;q=0.9", "Accept-Encoding": "gzip, deflate, br", "Accept-Language": "zh-CN", "Cache-Control": "max-age=0", "Connection": "keep-alive", "Content-Type": "application/x-www-form-urlencoded", "Cookie": "MESSAGE_TICKET=%7B%22times%22%3A0%7D; ", "Host": "cas.hrbeu.edu.cn", "Referer": "https://cas.hrbeu.edu.cn/cas/login?service=http%3A%2F%2Fjkgc.hrbeu.edu.cn%2Finfoplus%2Flogin%3FretUrl%3Dhttp%253A%252F%252Fjkgc.hrbeu.edu.cn%252Finfoplus%252Fform%252FJSXNYQSBtest%252Fstart", "Upgrade-Insecure-Requests": "1", "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/70.0.3538.102 Safari/537.36 Edge/18.18362" }
data = { "username":"studentNumber", "password":"password" } def findStr(source, target): return source.find(target) != -1 title = "" msg = ""
try: url_login = 'https://cas.hrbeu.edu.cn/cas/login?service=http%3A%2F%2Fjkgc.hrbeu.edu.cn%2Finfoplus%2Fform%2FJSXNYQSBtest%2Fstart' print("Begin to login ...") sesh = requests.session() req = sesh.get(url_login) html_content = req.text
login_html = lxml.html.fromstring(html_content) hidden_inputs=login_html.xpath(r'//div[@id="main"]//input[@type="hidden"]') user_form = {x.attrib["name"] : x.attrib["value"] for x in hidden_inputs}
user_form["username"]=data['username'] user_form["password"]=data['password'] user_form["captcha"]='' user_form["submit"]='登 录' headers['Cookie'] = headers['Cookie'] + req.headers['Set-cookie']
req.url = f'https://cas.hrbeu.edu.cn/cas/login;jsessionid={req.cookies.get("JSESSIONID")}?service=http%3A%2F%2Fjkgc.hrbeu.edu.cn%2Finfoplus%2Fform%2FJSXNYQSBtest%2Fstart' response302 = sesh.post(req.url, data=user_form, headers=headers) casRes = response302.history[0] print("CAS response header", findStr(casRes.headers['Set-Cookie'],'CASTGC'))
jkgc_response = sesh.get(response302.url)
headers['Accept'] = '*/*' headers['Cookie'] = jkgc_response.request.headers['Cookie'] headers['Host'] = 'jkgc.hrbeu.edu.cn' headers['Referer'] = jkgc_response.url jkgc_html = lxml.html.fromstring(jkgc_response.text) csrfToken = jkgc_html.xpath(r'//meta[@itemscope="csrfToken"]') csrfToken = csrfToken.pop().attrib["content"] jkgc_form = { 'idc': 'JSXNYQSBtest', 'release': '', 'csrfToken': csrfToken, 'formData': { '_VAR_URL': jkgc_response.url, '_VAR_URL_Attr': { 'ticket': re.match(r'.*ticket=(.*)', jkgc_response.url).group(1) } } } jkgc_form['formData'] = json.dumps(jkgc_form['formData']) jkgc_url = 'http://jkgc.hrbeu.edu.cn/infoplus/interface/start' response3 = sesh.post(jkgc_url, data=jkgc_form, headers=headers)
form_url = json.loads(response3.text)['entities'][0] form_response = sesh.get(form_url)
headers['Accept'] = 'application/json, text/javascript, */*; q=0.01' headers['Referer'] = form_url headers['X-Requested-With'] = 'XMLHttpRequest' submit_url = 'http://jkgc.hrbeu.edu.cn/infoplus/interface/doAction'
submit_html = lxml.html.fromstring(form_response.text) csrfToken2 = submit_html.xpath(r'//meta[@itemscope="csrfToken"]') csrfToken2 = csrfToken2.pop().attrib["content"]
submit_form = { 'actionId': '1', 'boundFields': 'fieldCXXXdqszdjtx,fieldCXXXjtgjbc,...', 'csrfToken': csrfToken2, 'formData': r'{"_VAR_EXECUTE_INDEP_ORGANIZE_Name":"学院","_VAR_ACTION_INDEP_ORGANIZES_Codes":"xxxxx",...}', 'lang': 'zh', 'nextUsers': '{}', 'rand': str(random.random() * 999), 'remark': '', 'stepId': re.match(r'.*form/(\d*?)/',form_response.url).group(1), 'timestamp': str(int(time.time()+0.5)) } response_end = sesh.post(submit_url, data=submit_form, headers=headers) resJson = json.loads(response_end.text)
print('Form url: ', form_response.url) print('Form Status: ', resJson['ecode']) print('Form stJson: ', resJson)
if (resJson['errno'] == 0): print('Form Succeed: ', resJson['ecode']) title = f'打卡成功 <{submit_form["stepId"]}>' msg = '\t表单地址: ' + form_response.url + '\n\n\t表单状态: \n\t\terrno:' + str(resJson['errno']) + '\n\t\tecode:' + str(resJson['ecode']) + '\n\t\tentities:' + str(resJson['entities']) + '\n\n\n\t完整返回:' + response_end.text else: print('Form Error: ', resJson['ecode']) title = f'打卡失败!内网出错' msg = '\t表单地址: ' + form_response.url + '\n\n\t错误信息: \n\t\terrno:' + str(resJson['errno']) + '\n\t\tecode:' + str(resJson['ecode']) + '\n\t\tentities:' + str(resJson['entities']) + '\n\n\n\t完整返回:' + response_end.text except: print('\n:.:.:.:.: Except return :.:.:.:.:') err = traceback.format_exc() print('Python Error: \n', err) title = '打卡失败!脚本出错' msg = '\t脚本报错: \n\n\t' + err finally: print('\n:.:.:.:.: Finally :.:.:.:.:')
from email.mime.text import MIMEText from email.header import Header mail_host="smtp.exmail.qq.com" mail_user="example@example.com" mail_pass="emailpassword" sender = '1@example.com' receivers = ['2@example.com'] message = MIMEText(msg, 'plain', 'utf-8') message['From'] = Header("1@example.com", 'utf-8') message['To'] = Header("2@example.com", 'utf-8') subject = title message['Subject'] = Header(subject, 'utf-8') try: smtpObj = smtplib.SMTP_SSL(mail_host) smtpObj.connect(mail_host, 465) smtpObj.login(mail_user,mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) print ("Success: The email was sent successfully") except smtplib.SMTPException: print ("Error: Can not send mail")
|
评论