Index:
1、考试思维→工作思维
2、requests和session的请求区别
1、考试思维→工作思维
工作思维就是解决问题的思维,以终为始,逆向思考;
(1)工作往往是条件不全具备就开始做,边做边学;
(2)工作可以使用抄袭、沟通、搜索等一切手段完成你负责的任务;
2、requests和session的请求区别
requests:生命周期短,不可保持请求,占用服务器资源少;每一次发起都当成是另一个人在打开一个新的登录界面;每一次客户端收到response后,服务器的session就被立马释放掉了;
session:生命周期长,可保持请求,占用服务器资源多;每一次发起都当成是同一个人在进行同一个会话的连续操作;每一次客户端收到response后,服务器的session都会继续留存在会话周期(30分钟内);
代码示例1:
# coding=utf-8
# encoding: utf-8
"""
Author:刘源
Createtime:2021/09/25 14:33:32
Updatetime:2021/09/25 14:33:32
Description:测开训练营公开讲堂第四期
"""
import time,re,requests
data=''
# 一系列文件操作都可以这么处理,某个文件下全部如此处理,批量文件处理工具
with open('pythonTD4.txt', 'r') as f:
for line in f.readlines():
newline=re.compile(r'\.py.*运行记录').sub('.py历史运行记录',line)
data += newline
with open('pythonTD4.txt', 'w') as f:
f.write(data)
f.write(f'{__file__}最新运行记录:')
f.write(time.ctime())
f.write('---' + str(time.time()) + '\n')
# 先查询登录页面的token,正则法(因为博主python3.9暂时没有适配版本的lxml模块,所以此处用正则抓token)
print("---002---")
url='http://boweifeng.xueqingyun.com/user/sign-in/login'
response=requests.get(url).text
# print(response)
csrf_token=re.compile(r'<.*token.*>').findall(response)[0] #抓取到token那一行
print(csrf_token)
csrf_token1=re.compile(r'.*content="').sub('',csrf_token) #缩小范围,取content=后的值
print(csrf_token1)
csrf=re.compile(r'">').sub('',csrf_token1) #去掉">,得到我们要的token(csrf)
print("---003---",csrf)
# 然后再带token登录,结果还是失败了,因为不同的requests对应不同的token,相当于又开了一个登录页面
# 页面上实际是一次request做了2个步骤:获得token,带token发送post型request
# 此时需要requests模块的的session保持机制,多次步骤合成一个request
print("---004---")
url2='http://boweifeng.xueqingyun.com/user/sign-in/login'
userinfo={'_csrf':csrf,
'LoginForm[identity]':'xzmadmin',
'LoginForm[password]':'51testing',
'LoginForm[rememberMe]': '1',
'login-button':''
}
response2=requests.post(url=url2,data=userinfo).text
print("---005---",response2)
代码示例2:
# coding=utf-8
# encoding: utf-8
"""
Author:刘源
Createtime:2021/09/25 14:33:32
Updatetime:2021/09/25 14:33:32
Description:
"""
import time,re,requests
data=''
# 一系列文件操作都可以这么处理,某个文件下全部如此处理,批量文件处理工具
with open('pythonTD4.txt', 'r') as f:
for line in f.readlines():
newline=re.compile(r'\.py.*运行记录').sub('.py历史运行记录',line)
data += newline
with open('pythonTD4.txt', 'w') as f:
f.write(data)
f.write(f'{__file__}最新运行记录:')
f.write(time.ctime())
f.write('---' + str(time.time()) + '\n')
# 先查询登录页面的token,正则法(因为博主python3.9暂时没有适配版本的lxml模块,所以此处用正则抓token)
# requests模块的的session保持机制
print("---002---")
url='http://boweifeng.xueqingyun.com/user/sign-in/login'
requests_session=requests.session() #requests模块的的session保持机制的一个实例化
response=requests_session.get(url).text
# print(response)
csrf_token=re.compile(r'<.*token.*>').findall(response)[0] #抓取到token那一行
print(csrf_token)
csrf_token1=re.compile(r'.*content="').sub('',csrf_token) #缩小范围,取content=后的值
print(csrf_token1)
csrf=re.compile(r'">').sub('',csrf_token1) #去掉">,得到我们要的token(csrf)
print("---003---",csrf)
# 然后再带token登录,requests模块的的session保持机制
print("---004---")
url2='http://boweifeng.xueqingyun.com/user/sign-in/login'
userinfo={'_csrf':csrf,
'LoginForm[identity]':'xzmadmin',
'LoginForm[password]':'51testing',
'LoginForm[rememberMe]': '1',
'login-button':''
}
response2=requests_session.post(url=url2,data=userinfo).text
print("---005---",response2)
补充截图: