In Python's requests library, the Session object provides a more advanced way to manage requests. Through the session, you can maintain certain specific parameters or states between multiple requests, such as shared cookies, HTTP headers, and connection pools, which makes it more convenient and efficient to handle scenarios that require multiple requests.

Main advantages

  1. Keep cookies in session: Cookies can be shared between multiple requests, which is very useful in scenarios where you need to maintain user login status or other session information.
  2. Shared headers: In a session, global headers can be set so that you don't need to repeat the same headers in each request.
  3. Persistent connection: The session object maintains a persistent TCP connection in the background, thereby improving request performance, especially when a large number of requests need to be sent.

Basic usage of requests Session

Creating a session object

Use requests.Session() to create a session object:

import requests

session = requests.Session()

Sending Requests in a Session

You can use the session object to send various types of requests (GET, POST, PUT, etc.). The session maintains shared state (such as cookies) between multiple requests.

response = session.get('https://httpbin.org/get')
print(response.text)

Set session-level headers

You can set global headers in a session, and all requests sent through this session will automatically carry these headers.

session.headers.update({'User-Agent': 'my-program'})

response = session.get('https://httpbin.org/headers')
print(response.json())

You can still set parameters for each request individually:

response = session.get('https://httpbin.org/headers', headers={'A': 'b'})

Session cookies

Sessions automatically manage and share cookies between requests. When you send a request to a website and receive cookies, subsequent requests will automatically carry those cookies.

response = session.get('https://httpbin.org/cookies/set/token/123456')

response = session.get('https://httpbin.org/cookies')
print(response.json())

Persist specific URL parameters

Sessions can preset URL parameters, and all requests will carry these parameters.

session.params = {'from': 'perfcode.com'}

response = session.get('https://httpbin.org/get')
print(response.url)
https://httpbin.org/get?from=perfcode.com

Authentication

Sessions support global authentication settings, suitable for APIs that require identity authentication.

session.auth = ('user', 'pass')

response = session.get('https://httpbin.org/basic-auth/user/pass')
print(response.status_code)

Close the session

After using the session, you can close it through the close() method to release the connection resources.

session.close()

Using Sessions with Context Managers

You can use context managers to automatically manage session creation and closing.

import requests

session = requests.Session()

with requests.Session() as session:
    response = session.get('https://httpbin.org/get')
    print(response.text)