当前位置:网站首页>requests. The difference between session () sending requests and using requests to send requests directly
requests. The difference between session () sending requests and using requests to send requests directly
2022-06-30 06:01:00 【bckBCK】
requests.session() Send a request And use requests The difference between sending a request directly
requests.session() Send a request And use requests The difference between sending a request directly
One 、Session
stay requests in ,session Object is a very common object , This object represents a user session : Start by connecting to the server from the client browser , Disconnect from server to client browser .
Sessions allow us to keep certain parameters across requests , Like in the same session Keep... Between all requests made by the instance cookie Information .
1、 establish session object
session = requests.session()
1
obtain session After object , You can call the method in the object to send the request .
response1 = session.get(url,params,headers)
response2 = session.post(url,data,json,headers)
1
2
adopt session To send the get、post、delete、put Wait for requests and get responses .
Two 、requests
requests yes Python A third-party library for , Mainly used to send network requests , such as get、post And other requests have achieved the purpose of obtaining network response
The grammar is as follows :
import requests
response1 = requests.get(url,params,headers,cookies) # send out get Network request
response2 = requests.post(url,data,json,headers,cookies) # send out post Network request
put、delete Similar to other request methods
1
2
3
4
3、 ... and 、session Objects and requests The difference between the requests sent by the two methods :
1、 scene
Log in to a mall
Query my order data
2、 Business code analysis
First of all, there are two interfaces involved , One “ Login interface ”, The other is “ Query order ” The interface of .
The normal operation is that we By calling the login interface To get the response cookie Information .
And then take this cookie Information as a parameter for the next request (cookie With the information of the current login ) To request Interface for querying orders
The general code is as follows :
The following code is purely for example , Useless pseudo code
import requests
Login interface
response1 = requests.get(url_login,params,headers)
obtain cookies Information
cookies = response.cookies
Got cookies It's a dictionary type
cookie = cookies.get(“cookies Of key”)
request Query interface
response2 = requests.get(search_url,params,headers,cookies=cookie)
View the results of the query response
response2.json()
1
2
3
4
5
6
7
8
9
10
11
12
Use session The code is as follows :
The following code is purely for example , Useless pseudo code
import requests
obtain session object
session = requests.session()
Login interface
response1 = session.get(url_login,params,headers)
request Query interface
response2 = session.get(search_url,params,headers)
View the results of the query response
response2.json()
1
2
3
4
5
6
7
8
9
10
difference :
Through the comparison of codes, it can be found that session Object efficiency will be better , Not every time cookie The information is placed in the request content
session Object can automatically get cookie And we can automatically bring what we get in the next request cookie Information , There is no need to fill in
Supplementary information :
From the return value of the request method response What you can get from :
response.status_code Status code
response.url request url
response.encoding Check the response header character encoding
response.cookies cookie Information
response.headers Header information
response.text Response content in text form
response.content Response contents in binary bytes
response.json() JSON Response content of form ( In fact, that is dict Dictionary type )
————————————————
Copyright notice : This paper is about CSDN Blogger 「6 sir 6」 The original article of , follow CC 4.0 BY-SA Copyright agreement , For reprint, please attach the original source link and this statement .
Link to the original text :https://blog.csdn.net/qq_25986923/article/details/105332640
边栏推荐
- 领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!
- [MD editing required] welcome to the CSDN markdown editor
- 1380. lucky numbers in matrices
- Leetcode search insert location
- Luogup2756 pilot pairing scheme problem (maximum flow)
- inno setup 最简单的自定义界面效果
- Cisco vxlan configuration
- 10-【istio】istio Sidecar
- Sword finger offer 22 The penultimate node in the linked list
- 旋转标注工具roLabelImg
猜你喜欢

Mysql database user management

Solitidy - fallback 回退函数 - 2种触发执行方式
![[ansible series] fundamentals -01](/img/b4/1f3284338c75acb5259849a45bbfbe.jpg)
[ansible series] fundamentals -01
![[ansible series] fundamentals 02 module debug](/img/99/c53be8e2a42c7cb5b4a9a7ef4ad98c.jpg)
[ansible series] fundamentals 02 module debug

Sword finger offer 22 The penultimate node in the linked list

Basic operations of C language

Codeforces B. MEX and Array

CompletableFuture从了解到精通,你想知道的这里都有

Leetcode56. consolidation interval

Who doesn't want a blog site of their own - build a blog site WordPress
随机推荐
Do you know how to show the health code in only 2 steps
Create uiactionsheet [duplicate] - creating uiactionsheet [duplicate]
Set of XXL job principles
MySQL log management, data backup and recovery
Using lazy < t > in C # to realize singleton mode in WPF
[MD editing required] welcome to the CSDN markdown editor
583. 两个字符串的删除操作-动态规划
从零开发 stylelint规则(插件)
Summation of basic exercise sequence of test questions
Dao -- a beautiful new world?
[exercise] basic practice letter graph of Blue Bridge Cup
[secretly kill little partner pytorch20 days] - [day4] - [example of time series data modeling process]
1380. lucky numbers in matrices
SHELL
Promise knowledge points
Codeforces B. MEX and Array
雲服務器部署 Web 項目
Summary of redis learning notes (I)
At the beginning of 2022, people who are ready to change jobs should pay attention to
Projet Web de déploiement du serveur Cloud