Session Objects and Cookie Persistence
In this tutorial, we are going to understand two key concepts in HTTP/HTTPs protocol - Session Objects and Cookie Persistence. These are advanced concepts in Python's requests
library that can greatly simplify and optimize our code when dealing with HTTP requests.
Session Objects in Requests
A Session object in requests
is a persistent session across multiple requests. When you're making several requests to the same host, the underlying TCP connection is reused, which can result in a significant speed-up.
Here's a simple way to use a Session object:
import requests
s = requests.Session()
s.get('http://httpbin.org/cookies/set/sessioncookie/123456789')
r = s.get('http://httpbin.org/cookies')
print(r.text)
In the example above, we first create a Session object, s
. Then we use s
to send GET requests. Since we're using a Session, requests
will handle the cookie sharing between these two requests.
Cookie Persistence in Sessions
As you can see from the example above, cookies are persisted across requests in a session. When we sent a request to 'http://httpbin.org/cookies/set/sessioncookie/123456789', the server set a cookie named 'sessioncookie'. In the next request, this cookie was automatically included, because we're using the same Session.
We can also manually add cookies to a Session:
s = requests.Session()
s.cookies.update({'cookie_name': 'cookie_value'})
Now, any request sent using the session s
will include the cookie 'cookie_name'.
Session Configuration
Sessions can also be used to set default data to the request methods. This is done by providing data to the properties on a Session object:
s = requests.Session()
s.auth = ('user', 'pass')
s.headers.update({'x-test': 'true'})
# both 'x-test' and 'x-test2' are sent
s.get('http://httpbin.org/headers', headers={'x-test2': 'true'})
In this example, we first set the authentication and headers for the session. These will be used as the defaults for any requests sent with this Session. When we send a GET request with an additional header, both the session header and the request header are included.
Conclusion
Session objects are a powerful tool in requests
, allowing for TCP connection reuse and cookie persistence across requests. This can greatly simplify your code when dealing with multiple requests to the same host.
Remember, understanding and using these advanced concepts can help you write more efficient and cleaner code. Happy coding!