HTTP协议无状态,该怎么解决?
2023-06-13 09:17:07 时间
HTTP协议无状态,书面点的说法是指协议对于交互性场景没有记忆功能,直白点的说,就是HTTP刷新后,不记得你之前做了什么设置,通常要解决cookie记录登录状态的方法有以下几种:
1.直接把用户名和密码保持到cookie中,不过目前此类方法风险比较大,一般不把密码等重要信息保存到Cookie中。
2.把密码加密后保存到Cookie中,下次访问时解密并与数据库比较。
3.加密账号后连同账号一块保存到Cookie中。
4.利用session会话将cookies等值自动传到下一个接口。Session相当于程序在服务器上建立的一份客户档案,客户来访的时候只需要查询客户档案表就可以了。
手动传cookies有一定的繁琐:
import requests
# 登录接口
log_url = "http://shopxo.hctestedu.com/index.php?s=/index/user/login.html"
# 登录参灵敏
data = {"accounts": "longge",
"pwd": "888888"}
# 头部信息
head = {"X-Requested-With": "XMLHttpRequest"}
login_response = requests.post(log_url, data=data, headers=head)
# 获取cookie,传给下面订单接口
cookie = login_response.cookies
# 订单接口
order_url = "http://shopxo.hctestedu.com/index.php?s=/index/cart/index.html"
order_response = requests.get(order_url, cookies=cookie)
print(order_response.text)
不过我们也可以尝试session会话管理,订单接口自动带上cookeis,这样我们就可以保持会话,将cookies等值自动传到下一个接口:
import requests
# 登录接口
log_url = "http://shopxo.hctestedu.com/index.php?s=/index/user/login.html"
# 登录参灵敏
data = {"accounts": "longge",
"pwd": "888888"}
# 头部信息
head = {"X-Requested-With": "XMLHttpRequest"}
# 实例化session对像
session = requests.session()
login_response = session.post(log_url, data=data, headers=head)
# 订单接口
order_url = "http://shopxo.hctestedu.com/index.php?s=/index/cart/index.html"
# 不用手动传入cookies值,因为session自动带上了
order_response = session.get(order_url)
print(order_response.text)
# 打印cookies
print(session.cookies)
相关文章
- Http通过header传递参数_http contenttype
- Http实战之无状态协议、keep-alive分析
- Go HTTP 编程 | 01 - 使用 http 包创建 Web 服务器
- 远程端口、Web端口看到SYN_RECEIVED状态要注意,可能是泛洪攻击
- HTTP 响应状态码全解
- HTTP状态 500 - 内部服务器错误,Servlet[SpringMVC]的Servlet.init()引发异常
- 安灯(andon)系统如何实时了解机台与工位状态
- Http常见状态码说明详解程序员
- 分析掌握Oracle 数据库实时状况:系统级状态分析(oracle数据库状态)
- MySQL Status Com_stmt_reprepare 数据库状态作用意思及如何正确
- MySQL Status Innodb_data_reads 数据库状态作用意思及如何正确
- MySQL Status Rpl_semi_sync_master_net_waits 数据库状态作用意思及如何正确
- 服务器一步步教你搭建Linux HTTP服务器(linux搭建http)
- Linux下HTTP抓包实战精要(http抓包linux)
- 极速搭建Linux下HTTP代理服务器(linux搭建http代理)
- HTTP状态码详解
- 服务器管理Linux中的HTTP服务器(linux中http)
- 深入浅出:Linux 架构学习 HTTP 协议(http协议linux)
- MSSQL 服务器运行状态监控实践(mssql 运行状态)
- Linux下快速访问HTTP(linux 访问http)
- 使用CMD在Windows中检测MySQL服务状态(cmd 检测mysql)