Using Session object with Python Requests

The Requests Session object allows you to persist specific parameters across requests to the same site. To get the Session object in Python Requests, you need to call the requests.Session() method. The Session object can store such parameters as cookies and HTTP headers. In addition, the Session object allows you to reuse open TCP connections and significantly improve the performance of network requests because you don't have to re-establish a TCP connection each time you send a request. The Session object includes all the methods of the main Requests API. In this Python Requests Session example, we store custom headers and authentication data in a Sessions object. Click Execute to run Python Requests Session Example online and see the result.
Using Session object with Python Requests Execute
import requests

s = requests.Session()
s.headers.update({'Authorization': 'Bearer {token}'})

r = s.get('https://reqbin.com/echo/get/json')

print(f'Status Code: {r.status_code}, Content: {r.json()}')
Updated: Viewed: 19695 times

What is the Python Request Library?

Requests Library is a popular library for sending HTTP requests in Python. The Requests Library simplifies the process of working with HTTP requests in Python. The Requests library is flexible and can send any type of HTTP request using POST, GET and DELETE methods, post JSON and XML, submit HTML forms and upload files. The code is written using the Python Requests Library, easy to read and maintain. ​Although Python has a built-in urllib3 module for handling HTTP requests with similar functionality, almost everyone uses the Requests library because of its simplicity and convenience.

How to install the Python Requests Library?

To install the Python Requests Library, you need to run the following command:

Install Python Requests Library
pip install requests

After installing the Requests Library, you can use it in your code by importing the Requests Library with the following Python code:

import requests

How to set headers in Python Request Session object?

The following is an example of setting a header for a session object in Python:

Python Set Headers on the Request Session Object Example
import requests

s = requests.Session()

s.headers.update({'Accept': 'application/json'})

r = s.get('https://reqbin.com/echo/get/json')

print(f'Status Code: {r.status_code}, Content: {r.json()}')

How to set authentication data for Python Request Session object?

The following is an example of setting authentication data for a session object in Python:

Python Authentication for the Request Session Object Example
import requests

s = requests.Session()
s.auth = ('login', 'password')

r = s.post('https://reqbin.com/echo/post/json')

print(f'Status Code: {r.status_code}')


What is the Python Request Library?

Python Requests is a popular library for sending HTTP requests in Python. The Requests Library simplifies the process of working with HTTP requests in Python. The Requests library is flexible and can send any type of HTTP request using POST, GET and DELETE methods, post JSON and XML, submit HTML forms and upload files. The code is written using the Python Requests Library, easy to read and maintain. ​Although Python has a built-in urllib3 module for handling HTTP requests with similar functionality, almost everyone uses the Requests library because of its simplicity and convenience.

How to install the Python Requests Library?

To install the Python Requests Library, you need to run the following command:

Install Python Requests Library
pip install requests

After installing the Requests Library, you can use it in your code by importing the Requests Library with the following Python code:

import requests

How to set headers in Python Request Session object?

The following is an example of setting a header for a session object in Python:

Python Set Headers on the Request Session Object Example
import requests

s = requests.Session()

s.headers.update({'Accept': 'application/json'})

r = s.get('https://reqbin.com/echo/get/json')

print(f'Status Code: {r.status_code}, Content: {r.json()}')

How to set authentication data for Python Request Session object?

The following is an example of setting authentication data for a session object in Python:

Python Authentication for the Request Session Object Example
import requests

s = requests.Session()
s.auth = ('login', 'password')

r = s.post('https://reqbin.com/echo/post/json')

print(f'Status Code: {r.status_code}')

How to set a proxy for Python Request Session object?

The following is an example of setting a proxy for request session object in Python:

Python Proxy for the Requests Session Object Example
import requests

s = requests.Session()
  
s.proxies = {
  'https': '10.10.10.10:3128',
}
  
r = s.get('https://reqbin.com/echo')
  
print(f'Status Code: {r.status_code}')

How to pass Authorization header to Python Request Session object?

The following is an example of passing an authorization header to a requests session object in Python:

Python Authorization Header for the Requests Session Object Example
import requests

s = requests.Session()
s.headers.update({'Authorization': 'Bearer {token}'})

r = s.get('https://reqbin.com/echo/get/json')

print(f'Status Code: {r.status_code}, Content: {r.json()}')

How to add cookies to Request Session object in Python?

The following is an example of adding a cookie to request session object in Python:

Python Add Cookies to the Request Session Object Example
import requests

s = requests.Session()

s.cookies.set("COOKIE_NAME", "The Cookie", domain="https://reqbin.com/echo")

r = s.get('https://reqbin.com/echo/get/json')

print(f'Status Code: {r.status_code}')

What is the difference between session and cookies?

A cookie and a session are used to store information. Cookies are only stored on the client machine, and sessions are stored on both the client and the server. The session creates a file in a temporary directory on the server where the registered session variables and their values ​​are stored. This data will be available for all site pages during this visit. Typically, a session ends 30 minutes after the user leaves the site or closes the browser. Cookies are text files that are stored on the client's computer and are designed to track usage. The server script sends a set of cookies to the browser. Cookies are stored locally in the browser for future use.

See also

thon Request Session object?

The following is an example of setting a proxy for request session object in Python:

Python Proxy for the Requests Session Object Example
import requests

s = requests.Session()
  
s.proxies = {
  'https': '10.10.10.10:3128',
}
  
r = s.get('https://reqbin.com/echo')
  
print(f'Status Code: {r.status_code}')

How to pass Authorization header to Python Request Session object?

The following is an example of passing an authorization header to a requests session object in Python:

Python Authorization Header for the Requests Session Object Example
import requests

s = requests.Session()
s.headers.update({'Authorization': 'Bearer {token}'})

r = s.get('https://reqbin.com/echo/get/json')

print(f'Status Code: {r.status_code}, Content: {r.json()}')

How to add cookies to Request Session object in Python?

The following is an example of adding a cookie to request session object in Python:

Python Add Cookies to the Request Session Object Example
import requests

s = requests.Session()

s.cookies.set("COOKIE_NAME", "The Cookie", domain="https://reqbin.com/echo")

r = s.get('https://reqbin.com/echo/get/json')

print(f'Status Code: {r.status_code}')

See also