本文最后更新于 2024-10-28T21:26:49+08:00
由于寝室老是没电导致我的电脑处于工作状态开机,气死我了,我便想用python来了解目前的电费是多少并给我发邮件,首先我们要了解一下基本的python发送响应包的方法
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
| import requests
url = 'https://example.com' response = requests.get(url)
if response.status_code == 200: print("请求成功!") content = response.text print(content) else: print(f"请求失败,状态码:{response.status_code}") import requests
url = 'https://example.com/api' data = {'username': 'your_username', 'password': 'your_password'} response = requests.post(url, data=data)
if response.status_code == 200: print("POST 请求成功!") print(response.text) else: print(f"请求失败,状态码:{response.status_code}")
|
在了解之后我们用bp抓包,发现一个问题,我们不再微信程序时网页会有一个自校验的情况,让我们在微信中打开,这无疑是我们不希望的不过我们可以修改我们的请求头部伪造我们在微信中的情景
这是在User agent 修改,这样服务端便会认为我们在微信中了
1
| Mozilla/5.0 (Linux; Android 5.0; SM-N9100 Build/LRX21V) > AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 > Chrome/37.0.0.0 Mobile Safari/537.36 > MicroMessenger/6.0.2.56_r958800.520 NetType/WIFI
|
下面是我们的请求报文
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| POST /app/app/api/user/searchBindHouseListForMoney HTTP/1.1 Host: xdxfdf.xtu.edu.cn Content-Length: 58 Accept: application/json, text/javascript, */*; q=0.01 X-Requested-With: XMLHttpRequest Accept-Language: zh-CN User-Agent: Mozilla/5.0 (Linux; Android 5.0; SM-N9100 Build/LRX21V) > AppleWebKit/537.36 (KHTML, like Gecko) Version/4.0 > Chrome/37.0.0.0 Mobile Safari/537.36 > MicroMessenger/6.0.2.56_r958800.520 NetType/WIFI Content-Type: application/x-www-form-urlencoded; charset=UTF-8 Origin: http://xdxfdf.xtu.edu.cn Referer: http://xdxfdf.xtu.edu.cn/app/api/main/wxpay/payStepOne?XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Accept-Encoding: gzip, deflate, br Cookie: JSESSIONID=XXXXXXXXXXXXXXXXXXXXXXXX Connection: keep-alive
wxId=XXXXXXXXXXXXXXXXXXX&appId=XXXXXXXXXXXXXXXXXXXXXXX
|
在python中的写法便是
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
| import requests import smtplib
url = "https://xdxfdf.xtu.edu.cn/app/app/api/user/searchBindHouseListForMoney" headers = { "Cookie": "JSESSIONID=XXXXXXXXXXXXXXXXX", } data = { "wxId": "XXXXXXXXXXXXX", "appId": "XXXXXXXXXXXXX", } response = requests.post(url, headers=headers,data=data)
if response.status_code == 200: print("请求成功!") print("响应内容:", response.json()) else: print(f"请求失败,状态码:{response.status_code}") print("响应内容:", response.text)
|
可见重要的其实是cookie
我们的响应内容是(json转义后的)
1
| {'data': [{'customer_no': '00116607', 'house_id': 'xxxxxxxxxxxxxx', 'meter': [{'meter_status': None, 'customer_no': '00116607', 'sysId': None, 'wallet_balance': 0, 'house_addr': 'XXXX', 'meter_balance': 64.18, 'meter_type': '1', 'meter_balance_time': '20240914225456', 'buy_time': None, 'free_balance': None, 'house_no': '607', 'pay_type': '1', 'customer_name': 'xxxxxxx', 'meter_no': '000017001111', 'meter_id': 'xxxxxxxxxxxxxxxxxxxxx'}], 'house_no': '607', 'wallet_balance': 0, 'house_addr': 'xxxxxxxxxxxx', 'customer_name': 'XXXXXXXXXXXXX', 'pay_customer_flag': '0'}], 'msg': '', 'ret': '1'}
|
成功获取我们要的报文后我们了解了在python中存在一个交SMTP的库,自带的竟然是,直接贴使用代码,看得更清楚些
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
|
import smtplib from email.mime.text import MIMEText from email.header import Header
mail_host="smtp.XXX.com" mail_user="XXXX" mail_pass="XXXXXX" sender = '[email protected]' receivers = ['[email protected]'] message = MIMEText('Python 邮件发送测试...', 'plain', 'utf-8') message['From'] = Header("菜鸟教程", 'utf-8') message['To'] = Header("测试", 'utf-8') subject = 'Python SMTP 邮件测试' message['Subject'] = Header(subject, 'utf-8') try: smtpObj = smtplib.SMTP() smtpObj.connect(smtp.qq.com, 25) smtpObj.login(mail_user,mail_pass) smtpObj.sendmail(sender, receivers, message.as_string()) print "邮件发送成功" except smtplib.SMTPException: print "Error: 无法发送邮件"
|
我们需要开启qq邮箱的smtp服务,了解了
顺便贴一个From的格式我在这里卡住了。。。邮箱格式要正确呜呜呜