w3resource

Python: Send cookies and Access cookies of a server


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"
  }
}

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.



Follow us on Facebook and Twitter for latest update.