一、Cookie鉴权
http协议:无连接、无状态,请求无关联、独立
京东:登录、搜商品、下单、支付、评论
什么是Cookie?
cookie是在服务器产生的存储在客户端的一小段文本信息,格式是字典,键值对。
Cookie的分类
会话级:保存内存,当浏览器关闭就会丢失。
持久化:保存硬盘,只有当失效时间到了才会被清除。
如何查看Cookie
name、value、domain、path、expries.size
Cookie如何实现鉴权(原理)
当客户端第一次访问服务器时,那么服务器就会产生Cookie,然后通过响应头的方式在Set-Cookie传输到客户端,客户端从第2-N请求都会自动带上Cookie
致命弱点:cookie保存在客户端,对于敏感信息用户名、密码、身份证安全
"""
@Project: pytestDemo-master
@Description:Cookie鉴权
@Time: 2022/4/13 15:45
@Author:MING
"""
import re
import unittest
import requests
class Cookie_Login(unittest.TestCase):
csrf_token = ""
csrf_cookie = ""
# 第一次访问网站
def test_01_index(self):
url = 'http://47.107.116.139/phpwind'
res = requests.get(url=url)
# 通过正则表达式获取到csrf_token的值
value = re.search('name="csrf_token" value="(.+?)"', res.text)
Cookie_Login.csrf_token = value.group(1) # 获取csrf_token值
Cookie_Login.csrf_cookie = res.cookies # 获取cookie
print(value)
# 登录
def test_02_login(self):
url = "http://47.107.116.139/phpwind/index.php?m=u&c=login&a=dorun"
data = {
"username":"123456",
"password": "123456",
"csrf_token"