”;
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(''https://httpbin.org/cookies'',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(''https://httpbin.org/headers'', headers=headers) print(getdata.headers)
Advertisements
”;