Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Hint: You can adjust the default video playback speed in your account settings.
Hint: You can set your subtitle preferences in your account settings.
Sorry! Looks like there’s an issue with video playback 🙁 This might be due to a temporary outage or because of a configuration issue with your browser. Please refer to our video player troubleshooting guide for assistance.

The Session Object

If you want to persist certain parameters across multiple requests, you can make use of a session object. This lesson guides you through the process of creating a new session object and how to use it.

00:00 The Session object allows certain parameters to persist across multiple requests. The TCP connection that gets established will be reused, hence adding performance. When creating a Session object, it will have the same methods as the main requests API.

00:15 Let’s try it out by using the same authentication across multiple requests. So to start, we’re going to create a new file. Let’s name it session.py.

00:27 I’ll close the explorer.

00:32 To start a new script, we’re going to import requests. We’ll use that same password entry using the standard library getpass. So, a couple of comments here.

00:45 We’re going to use a context manager—using the statement with. That’s going to ensure the resources that are used during the session are going to be released after use. So to start that, we’ll say with requestshere’s where we’re going to create our Session object. And a very simple name, session.

01:03 Let’s begin by authenticating our session, and here’s where we’re going to be going to GitHub again. In this case, I’m using my example name—you would put your username for GitHub there—and then I’m going to have it prompt us for the password again.

01:20 So now that we’ve created the Session and set the authentication parameters, instead of requests.get() we’ll use session.get(). So, we’ll save it into response and here we’re going to grab our user information. And we could have multiple requests—we’ll try that here in a minute.

01:44 Let’s print out some information from it.

01:52 Let’s save, and then down here in the terminal, we’ll run it with session.py. It’s prompting us for the password.

02:05 I’ll make the terminal a little bit larger. What we can see here is after the Password:, it’s returning back our headers here, the date and the time… And after that, it’s returning JSON with all the information about that response.

02:24 Let’s try setting it up with a second request. So, we’ll call this one response2, and we’ll use the same session, but we’ll go to a different endpoint here, under /user, and we’ll look at the /repos. In fact, let’s print out a specific repo. We’ll say the very, very first repo—repo 0. And we’ll say, “What is the 'url'?”—one of the keys that we can call here.

02:55 All right. I’m going to save, I’m going to run it again.

03:01 And after it prints out the headers, after my Password: here, after it goes through all the headers for it, it’s going to go ahead and tell me where my first repo is. Great!

03:12 Let’s talk about another area of performance, max retries, in the next video.

abhinav on Jan. 7, 2021

While running this code I was getting a 401 error while trying to authenticate. Turns out that github has stopped authenticating with a username and password. docs.github.com/en/free-pro-team@latest/rest/overview/other-authentication-methods

Bartosz Zaczyński RP Team on Jan. 7, 2021

@abhinav Yes, that’s correct.

You need to generate a GitHub API token by following these steps:

  1. Log in to GitHub
  2. Navigate to github.com/settings/tokens
  3. Click “Generate new token”
  4. Give your token a name and check the scopes you might need.
  5. Copy your personal access token (e.g. 07ba312d2f3499d6526cd68g7b64a6dbb8e50bb6)

Then authenticate with your username and the token:

import requests

with requests.Session() as session:
    session.auth = ("jdoe", "07ba312d2f3499d6526cd68g7b64a6dbb8e50bb6")
    response = session.get("https://api.github.com/user")
    print(response.json())

abhinav on Jan. 7, 2021

@Bartosz Hey thanks a lot, that helped.

Sandhya on Jan. 4, 2023

Hi with session requests , I am getting getting message like Resetting dropped connection How I can handle this?

this is not breaking the script but it is not increasing the speed as expected.

rayroyal on Feb. 6, 2023

I need to understand how to terminate a session with logout. I also want to use cookie values during a session. How do I ensure session security. Please provide instruction on these topics.

Become a Member to join the conversation.