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.
import requests req = requests.Session() cookies = dict(test='test123') getdata = req.get('https://httpbin.org/cookies',cookies=cookies) print(getdata.text)
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 −
import requests req = requests.Session() req.headers.update({'x-user1': 'ABC'}) headers = {'x-user2': 'XYZ'} getdata = req.get('https://httpbin.org/headers', headers=headers) print(getdata.headers)