Python requests module session code example

The http protocol itself is stateless. In order to maintain state between requests, there are session and cookie mechanisms. requests also provides corresponding methods to manipulate them.

The session object in requests allows us to maintain certain parameters across HTTP requests, that is, the request header sent by the same session object carries a specified parameter. Of course, the most common application is that it can keep cookies in a subsequent series of requests.

Below, learn how to use it through the examples in the official documentation.

import requests
s = requests.Session()
# Step 1: Send a request to set cookies in the request
# tips: http://httpbin.org can be used to test http requests and responses
s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
# Step 2: Send another request to view the cookies in the current request
r = s.get("http://httpbin.org/cookies")print(r.text)

operation result

{" cookies":{"sessioncookie":"123456789"}}

From the results, we can see that the second request already carries the cookie set by the first request, that is, the purpose of keeping the cookie is achieved through the session. In the example, a requests.Session() object is created, through which the http request operation is performed, which is basically similar to requests.request()

Since the session provides continuity between requests, there is a difference between cross-request parameters and non-cross-request parameters. That is, sometimes I want all requests with a certain parameter, and sometimes I just want a single request with a temporary parameter. Learn how to use it through the following example.

import requests

s = requests.Session()
s.headers.update({'x-test':'true'})
# both 'x-test' and 'x-test2' are sent
r1 = s.get('http://httpbin.org/headers', headers={'x-test2':'true'})print(r1.text)
# ' x-test' is sent
r2 = s.get('http://httpbin.org/headers')print(r2.text)

operation result

# r1.text
{" headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"httpbin.org","User-Agent":"python-requests/2.22.0","X-Amzn-Trace-Id":"Root=1-5e91656f-b99f14a4d6f47f9e55a90bb4","X-Test":"true","X-Test2":"true"}}
# r2.text
{" headers":{"Accept":"*/*","Accept-Encoding":"gzip, deflate","Host":"httpbin.org","User-Agent":"python-requests/2.22.0","X-Amzn-Trace-Id":"Root=1-5e91656f-e9741db4c2ca2fd6e0628396","X-Test":"true"}}

From the results, we can draw two conclusions:

The session can provide default data for the request method. For example, {'x-test':'true'} in the first request is the default data, and the default data at this time is the cross-request parameter.
Method-level parameters will not be maintained across requests. For example, the second request does not carry headers={'x-test2':'true'}, and there is no {'x-test2':'true' in the returned result. }, indicating that the parameter has not been retained after the first request.

Reference materials

https://github.com/psf/requests/blob/master/requests/sessions.py

https://requests.readthedocs.io/en/master/user/advanced/#session-objects

The above is the whole content of this article, I hope it will be helpful to everyone's study.

Recommended Posts

Python requests module session code example
Python3 module
Python ATM function implementation code example
Python requests module cookie instance analysis
Python verification code interception identification code example
Python object-oriented example
Python Lesson 37-Module
Python3 built-in module usage
Python3.7 debugging example method
Python3 external module use
Python SMS bombing code
Prettytable module of python
Python interpolate interpolation example
Concurrent requests in Python (part 2)
Python code to find bugs (2)
Python code to find bugs(7)
How to comment python code
Python code to find bugs(4)
Python code to find bugs (3)
Python code to find bugs(9)
Python drawing rose implementation code
Python negative modulus operation example
Python implements code block folding
Python output mathematical symbols example
Python code to find bugs(6)
Python iterable object de-duplication example
Python code to find bugs (1)
Python code to find bugs(8)
Python implements verification code recognition
Python code to find bugs(5)
Python one-dimensional two-dimensional interpolation example
Is python code case sensitive
Python draw bar graph (bar graph) example
Python right alignment example method
matplotlib of python drawing module
Python Faker data forgery module