The difference between cookies and session

  • Cookie: The browser receives the set-cookie command from the server and saves the cookie on the computer. The cookie saved by each website can only be used on its own website
  • session: The data is stored on the server, and only an encrypted string of the associated data is placed in the cookie to mark

token

Application scenarios of token

  • Obtain the token with the authentication information, or configure the token through the background
  • Tokens are used in related requests, most of which are provided in the form of query parameters

The difference between session and token

  • token: A request field attached to a user request to verify identity and authority
  • session: can be based on cookies or query parameters, used to associate user-related data
  • For cross-terminal applications, such as the Android native system does not support cookies

    • Need to use token to identify users
    • Need to save the sessionid to the header or query field in the http request

Cookie handling

Ways to pass cookies

  • Pass through request header information
  • Pass the requested keyword parameter cookies

Pass cookies through header

import requests


def test_demo():
    url = "https://httpbin.testing-studio.com/cookies"
    header = {"Cookie": "name=leo"}
    r = requests.get(url=url, headers=header)
    print(r.request.headers)
    
# 打印结果
{'User-Agent': 'python-requests/2.25.1', 
 'Accept-Encoding': 'gzip, deflate', 
 'Accept': '*/*', 
 'Connection': 'keep-alive',
 'Cookie': 'name=leo'}

Pass by keyword parameter

import requests


def test_demo():
    url = "https://httpbin.testing-studio.com/cookies"
    header = {"User-Agent": "leo"}
    cookie = {
        "name": "leo",
        "address": "Foshan"
    }
    r = requests.get(url=url, headers=header, cookies=cookie)
    print(r.request.headers)
    
# 打印结果
{'User-Agent': 'leo', 
 'Accept-Encoding': 'gzip, deflate', 
 'Accept': '*/*', 
 'Connection': 'keep-alive', 
 'Cookie': 'address=Foshan; name=leo'}

\


机智的测试生活
88 声望478 粉丝

公号|机智的测试生活