python requests module-session python requests module-request api

The http protocol itself is stateless. In order to maintain state between requests, there is a session and cookie mechanism. 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 series of subsequent requests.

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

import requests

S = requests.Session ()
 # Step: sending a request, the request for setting Cookies 
# Tips: http://httpbin.org can be used to test the http request and response 
s.get ( ' http: // httpbin.org/cookies/set/sessioncookie/123456789 ' )
 # Step: a retransmission request for the current request to view the cookies 
R & lt s.get = ( " http://httpbin.org/cookies " )
 Print (r.text)

operation result

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

From the results, we can see that the second request has carried the cookie set by the first request, that is, the purpose of maintaining the cookie is achieved through the session. In the example, a requests.Session () object is created, and the http request operation is performed through the object. This operation is basically similar to requests.request (). You can check the requests module-request api of python for understanding.

Because the session makes the requests coherent, then there is a difference between cross-request parameters and non-cross-request parameters. That is, sometimes I want all requests to have a certain parameter, and sometimes I just want a single request with temporary parameters. Use the following example to understand how to use it.

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 the returned result does not contain {'x-test2': 'true' }, Indicating that the parameter was not retained after the first request.

References

 

Guess you like

Origin www.cnblogs.com/zhuosanxun/p/12679121.html