Session Objects – Python requests
Last Updated :
07 Jun, 2022
Session object allows one to persist certain parameters across requests. It also persists cookies across all requests made from the Session instance and will use urllib3’s connection pooling. So, if several requests are being made to the same host, the underlying TCP connection will be reused, which can result in a significant performance increase. A session object all the methods as of requests.
Using Session Objects
Let us illustrate the use of session objects by setting a cookie to a URL and then making a request again to check if the cookie is set.
Python3
import requests
s = requests.Session()
print (r.text)
|
Output One can check that cookie was still set when the request was made again. Sessions can also be used to provide default data to the request methods. This is done by providing data to the properties on a Session object:
Python3
import requests
s = requests.Session()
s.auth = ( 'user' , 'pass' )
s.headers.update({ 'x-test' : 'true' })
print (s)
|
Output
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...