w3resource

Python: Send cookies and Access cookies of a server

Python Requests: Exercise-8 with Solution

Write a Python code 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 Python skills with w3resource's quiz


Python: Tips of the Day

Creates a dictionary with the same keys as the provided dictionary and values generated by running the provided function for each value:

Example:

def tips_map_values(obj, fn):
  ret = {}
  for key in obj.keys():
    ret[key] = fn(obj[key])
  return ret
users = {
  'Owen': { 'user': 'Owen', 'age': 29 },
  'Eddie': { 'user': 'Eddie', 'age': 15 }
}

print(tips_map_values(users, lambda u : u['age'])) # {'Owen': 29, 'Eddie': 15}

Output:

{'Owen': 29, 'Eddie': 15}