Python: Send cookies and Access cookies of a server
8. Cookies Sender and Retriever
Write a Python program to send cookies to a given server and access cookies from the response of a server.
Sample Solution:
Python Code:
import requests
url = 'http://httpbin.org/cookies'
# A dictionary (my_cookies) of cookies to send to the specified url.
my_cookies = dict(cookies_are='Cookies parameter use to send cookies to the server')
r = requests.get(url, cookies = my_cookies)
print(r.text)
# Accessing cookies with Requests
# url = 'http://WebsiteName/cookie/setting/url'
# res = requests.get(url)
# Value of cookies
# print(res.cookies['cookie_name'])
Sample Output:
{ "cookies": { "cookies_are": "Cookies parameter use to send cookies to the server" } }
For more Practice: Solve these Related Problems:
- Write a Python program to send a request to a website with custom cookies using requests.get(cookies=...) and then print the cookies returned in the response.
- Write a Python function that sets cookies in a request, sends it to a specified URL, and returns the cookies from the response in a dictionary.
- Write a Python script to simulate a login process by sending a POST request with cookies, then retrieve and print the session cookies from the server.
- Write a Python program to test the persistence of cookies by sending multiple requests with the same cookies and comparing the responses.
Python Code Editor:
Have another way to solve this solution? Contribute your code (and comments) through Disqus.
Previous: Write a Python code to send some sort of data in the URL's query string.
Next: Write a Python code to verify the SSL certificate for a website which is certified.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.