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

Unlock This Lesson

This lesson is for members only. Join us and get access to hundreds 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 the default subtitles language 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 see our video player troubleshooting guide to resolve the issue.

The Session Object

Give Feedback

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.

Become a Member to join the conversation.