Requests Tutorial
Selected Reading
- Requests - Discussion
- Requests - Useful Resources
- Requests - Quick Guide
- Requests - Web Scraping using Requests
- Requests - Proxy
- Requests - Event Hooks
- Requests - Authentication
- Requests - SSL Certification
- Requests - Handling Sessions
- Requests - Handling History
- Requests - Handling Redirection
- Requests - Handling Timeouts
- Requests - Working with Errors
- Requests - Working with Cookies
- Requests - File Upload
- Handling POST, PUT, PATCH & DELETE Requests
- Requests - Handling GET Requests
- Requests - HTTP Requests Headers
- Handling Response for HTTP Requests
- Requests - Working with Requests
- Requests - How Http Requests Work?
- Requests - Environment Setup
- Requests - Overview
- Requests - Home
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Requests - Handling Sessions
Requests - Handpng Sessions
To maintain the data between requests you need sessions. So, if the same host is called again and again, you can reuse the TCP connection which in turn will improve the performance. Let us now see, how to maintain cookies across requests made using sessions.
Adding cookies using session
import requests req = requests.Session() cookies = dict(test= test123 ) getdata = req.get(,cookies=cookies) print(getdata.text)
Output
E:prequests>python makeRequest.py { "cookies": { "test": "test123" } }
Using session, you can preserve the cookies data across requests. It is also possible to pass headers data using the session as shown below −
Example
import requests req = requests.Session() req.headers.update({ x-user1 : ABC }) headers = { x-user2 : XYZ } getdata = req.get(, headers=headers) print(getdata.headers) Advertisements