How to Use Sessions in Python Requests for Persistent Connections
Use
requests.Session() to create a session object that keeps cookies and settings across requests. Call session.get() or session.post() to make requests that share the same session data.Syntax
To use sessions in Python requests, first create a session object with requests.Session(). Then use this session object to make HTTP requests like session.get() or session.post(). This keeps cookies and headers persistent across calls.
session = requests.Session(): creates a session objectsession.get(url): sends a GET request using the sessionsession.post(url, data): sends a POST request using the session
python
import requests session = requests.Session() response = session.get('https://example.com') print(response.status_code)
Output
200
Example
This example shows how to use a session to log in to a website and then access a page that requires login. The session keeps the login cookies so you don't have to log in again.
python
import requests # Create a session object session = requests.Session() # Login URL and credentials login_url = 'https://httpbin.org/post' login_data = {'username': 'user', 'password': 'pass'} # Send a POST request to login login_response = session.post(login_url, data=login_data) print('Login status:', login_response.status_code) # Access a page that requires login profile_url = 'https://httpbin.org/cookies' profile_response = session.get(profile_url) print('Profile page status:', profile_response.status_code) print('Cookies:', profile_response.text)
Output
Login status: 200
Profile page status: 200
Cookies: {
"cookies": {}
}
Common Pitfalls
Common mistakes when using sessions include:
- Not using a session object and making separate
requests.get()calls, which do not share cookies. - Forgetting to close the session if needed, which can keep connections open.
- Assuming session data is shared across different session objects (it is not).
Always reuse the same session object for related requests.
python
import requests # Wrong way: separate requests lose cookies response1 = requests.post('https://httpbin.org/post', data={'key': 'value'}) response2 = requests.get('https://httpbin.org/cookies') print('Cookies without session:', response2.text) # Right way: use session to keep cookies session = requests.Session() session.post('https://httpbin.org/post', data={'key': 'value'}) response = session.get('https://httpbin.org/cookies') print('Cookies with session:', response.text)
Output
Cookies without session: {
"cookies": {}
}
Cookies with session: {
"cookies": {}
}
Quick Reference
Summary tips for using sessions in Python requests:
- Create a session with
requests.Session(). - Use the session object to make all related requests.
- Session keeps cookies, headers, and connection pooling.
- Close the session with
session.close()when done.
Key Takeaways
Use requests.Session() to maintain cookies and settings across multiple requests.
Always reuse the same session object for related HTTP calls to keep state.
Sessions improve performance by reusing connections and managing cookies automatically.
Remember to close the session with session.close() when finished.
Avoid mixing session and non-session requests if you want to keep cookies.