In the previous examples, we called the interface or parsed the response individually, but in a real application scenario, we would often need to call several interfaces in succession. Such as:

  1. Log on first
  2. Let’s do some more
  3. The logout

Just like we did operations on a web page in a browser, Requests can simulate the steps of those operations.

Before we get started, let’s take a quick look at how the browser does this. Why is it that when we log in to a web site in the browser, the web site knows that we’re doing everything from the same account that we just logged in to?

Cookies and sessions

There are two concepts, one is cookies and one is session.

Cookies or Cookies are small text files that a Web server stores on a user’s browser. It can contain information about the user, such as a user name or even an encrypted password. This way, when we use the browser to access the server again, the server can read the information directly without the user having to type it again.

A Session, called a Session, usually jumps between web pages provided by the server to access different pages. The server creates a Session object for a user and stores it on the server so that the server knows who the user is. Sessions can also store user names and other information.

Session will expire automatically. Due to limited server resources, many websites will automatically delete your Session if you do not operate it for a period of time. At this time, the website will prompt you to log in again. And some sites, when they ask you to log in again, they will say “Welcome,” because the user name behind it is probably in your browser cookies, so when you try to log in again, the web will still know your user name, because cookies are not automatically deleted, by the way, Cookies are saved in the client, so we can set whether to save cookies or not. Many websites have set cookies that will prompt you that you cannot use the website if you do not save cookies.

Requests the cookies

Cookies are available in Requests, responses, and requests. If a response contains cookies, you can quickly access them (the following example can be run in interactive mode) :

Import requests URL =’www.baidu.com’ r=requests. Get (URL) Vars (r.cookies)

This allows you to see the cookie returned by the site, which vars (R.Cookies) convert to dict data type. To send your cookies to the server, you can use the cookies parameter (the server of the following sites will read the cookies sent by the user and return them in the text of the response body). :

Url =’httpbin.org/cookies’ cookies=dict (cookies are=’working) r=requests. Get (url, cookies=cookies) r.ext

‘{“cookies” : {“cookies are:”working”}} The return object of cookies is

RequestsCookieJar, which behaves like a dictionary but has a more complete interface and is suitable for use across domains and paths. You can also pass Cookie Jars to Requests:

Jar jar = requests. Cookies. RequestsCookieJar). The set (tasty cookies’, ‘yum’, domain = ‘httpbin.org’, Path =’/cookies’) jar.set (gross cookie’, ‘blech’, domain=’httpbin.org’, Path =’/elsewhere”) URL =’httpbin.org/cookies’ r=requests. Get (URL, cookies=jar) R.ext

{“cookies” : {“tasty cookie” : “yum}}’

Notice that the cookies that are typed here are only the ones that were set the first time. Because this access url matches the path entered in the first set. This example shows that the same RequestsCookieJar can store cross-domain and cross-path cookies. The server can retrieve the cookie value to determine which cookie value is valid.

Sessions in Requests

In Requests, there is also the concept of sessions, which is borrowed from the term session, but is not the same as sessions on the server side. Reqeusts is a cross-client tool, and it is obviously not possible to use it for cross-server sessions. However, we don’t want to manually pass cookies between a series of requests and responses, as in the cookies example above. The Requests session helps make this automatic. Example of the session in Example 1.Requests

The import requests s = requests. Session0 s.g et (http://httpbin.org/cookies/set/sessioncookie/123456789)" R = s.g et (" http:/httpbin.org/cookies ") print (r.t ext)Copy the code

In this example:

The second line creates a reqeusts session, and the third line accesses a url using GET. This GET request causes the server to create a server session object, and the url’s purpose is to return the server session content to the hash user. In line 4 the user gets another url and the server returns a “sessioncookie “: json data for” 123456789 “. This data is exactly the return value of the third line, which means that the server remembered us correctly.

Task 1. Let’s do a little exercise to impress.

  1. Observe the response given in line 3 of Example 1. (You can add r= before the third line and then observe the value of r.)
  2. Remove the third line from Example 1 and observe the result of running example 1.
  3. There’s a lot we can do with this session feature. For example, log in to a site with Requests, do something on the site, grab some data, and log out. Much of what we used to do with graphical automation libraries like Selenium can now be done through interfaces.