当前位置:网站首页>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
边栏推荐
- MySQL advanced SQL statement
- Mysql database learning notes - foreign keys, table connections, subqueries, and indexes for MySQL multi table queries
- Luogup2756 pilot pairing scheme problem (maximum flow)
- [exercise] basic practice letter graph of Blue Bridge Cup
- 二十四、输入输出设备模型(串口/键盘/磁盘/打印机/总线/中断控制器/DMA和GPU)
- MySQL 索引
- MySQL高级SQL语句
- Xi'an Jiaotong 21st autumn online expansion resources of online trade and marketing (III) [standard answer]
- leetcode763. Divide letter interval
- [MD editing required] welcome to the CSDN markdown editor
猜你喜欢

聲網,站在物聯網的“土壤”裏

Intelligent question - horse racing question

MySQL数据库用户管理
![[road of system analyst] collection of wrong topics in Project Management Chapter](/img/8b/2908cd282f5e505efe5223b4c5ddbf.jpg)
[road of system analyst] collection of wrong topics in Project Management Chapter
![[ansible series] fundamentals -01](/img/b4/1f3284338c75acb5259849a45bbfbe.jpg)
[ansible series] fundamentals -01

MySQL事物

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

Cisco VXLAN配置

云服务器部署 Web 项目

Sound net, debout dans le "sol" de l'IOT
随机推荐
09- [istio] istio service entry
Navigate back to fragmentpageradapter - & gt; Fragment is empty - navigating back to fragmentpageradapter - & gt; fragments are empty
[openstack]-01- basic introduction
Codeforces B. MEX and Array
Balanced binary tree judgment of Li Kou 110 -- classic problems
46. 全排列-dfs双百代码
Official win 10 image download
Qt之QListView的简单使用(含源码+注释)
About modifying dual system default startup item settings
领导:谁再用 Redis 过期监听实现关闭订单,立马滚蛋!
电脑查看WiFi使用密码
Promise knowledge points
After getting these performance test decomposition operations, your test path will be more smooth
Simple use of qlistview of QT (including source code + comments)
Finally someone can make the server so straightforward
Strlen and sizeof, array length and string length, don't be silly
从零开发 stylelint规则(插件)
VLAN access mode
leetcode763. Divide letter interval
Idea of capturing mobile terminal variant combination