当前位置:网站首页>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
边栏推荐
- 观察者模式、状态模式在实际工作中的使用
- Use of OpenCL thread algebra library viennacl
- Shenzhou ares tx6 boot logo modification tutorial
- The average salary of software testing in 2022 has been released. Have you been averaged?
- Prototype and prototype chain in JS
- Implementation of property management system with ssm+ wechat applet
- [deep learning] data segmentation
- Gestion des utilisateurs de la base de données MySQL
- [untitled] user defined function
- Idea of capturing mobile terminal variant combination
猜你喜欢

飞升:基于中文分词器IK-2种自定义热词分词器构建方式showcase & 排坑showtime

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

Mysql database learning notes - foreign keys, table connections, subqueries, and indexes for MySQL multi table queries
![[ansible series] fundamentals 02 module debug](/img/99/c53be8e2a42c7cb5b4a9a7ef4ad98c.jpg)
[ansible series] fundamentals 02 module debug

Here comes the nearest chance to Ali

UE4_ Editor UMG close window cannot destroy UMG immediately

重构之美:当多线程批处理任务挑起大梁 - 万能脚手架

MySQL advanced (Advanced SQL statement)

Transfer the token on the matic-erc20 network to the matic polygon

Intelligent question - horse racing question
随机推荐
Xi'an Jiaotong 21st autumn "computerized accounting" online homework answer sheet (I) [standard answer]
Using lazy < t > in C # to realize singleton mode in WPF
How to print pthread_ t - How to print pthread_ t
Gestion des utilisateurs de la base de données MySQL
Sword finger offer 22 The penultimate node in the linked list
What kind of answer has Inspur given in the big AI model landing test?
1380. lucky numbers in matrices
At the beginning of 2022, people who are ready to change jobs should pay attention to
Implementation of property management system with ssm+ wechat applet
[Alibaba cloud] student growth plan answers
[secretly kill little partner pytorch20 days] - [day4] - [example of time series data modeling process]
[MD editing required] welcome to the CSDN markdown editor
ES6数组遍历与ES5数组遍历
Codeforces Round #390 (Div. 2) D. Fedor and coupons
[OSPF] comparison between rip and OSPF
领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!
PC viewing WiFi password
24、 I / O device model (serial port / keyboard / disk / printer / bus / interrupt controller /dma and GPU)
股票在网上开户安全吗?在网上能不能开户炒股呢?
Force deduction exercise -- deleting repeated items in ordered sequence 1.0