python requests cookies

Python Requests Cookies

When working with web applications, it is common to interact with cookies. Cookies are small pieces of data that are sent from a website and stored in the user's web browser. Cookies can be used to keep track of user sessions, remember user preferences and store data across multiple page visits. Python's Requests library provides an easy way to work with cookies in HTTP requests and responses.

Sending Cookies Using Requests

To send cookies in a request, you can pass a dictionary of cookie values to the headers parameter of the request. The dictionary should contain the cookie name as the key and the cookie value as the value. Here is an example:


import requests

url = 'https://www.example.com'
cookies = {'cookie_name': 'cookie_value'}

response = requests.get(url, headers={'Cookie': '; '.join([f'{k}={v}' for k, v in cookies.items()])})

print(response.content)

Here, we are sending a GET request to the URL 'https://www.example.com' with a cookie named 'cookie_name' and value 'cookie_value'. The headers parameter takes a dictionary of HTTP headers that will be sent in the request. We use the 'Cookie' header to send the cookie values. The '; '.join() function is used to join the key-value pairs of the cookies dictionary into a single string.

Receiving Cookies Using Requests

When you send a request to a website, the website may respond with one or more cookies. These cookies can be accessed from the response object returned by Requests.


import requests

url = 'https://www.example.com'
response = requests.get(url)

cookies = response.cookies
for cookie in cookies:
    print(cookie.name, cookie.value)

In this example, we are sending a GET request to the URL 'https://www.example.com'. We then access the cookies attribute of the response object to get a list of cookies. We can iterate over this list to print out the name and value of each cookie.

Session Objects

Requests also provides a Session object that can be used to persist cookies across multiple requests. When you make a request using a Session object, any cookies received in the response will be stored in the Session object and sent with all subsequent requests made with that Session.


import requests

url = 'https://www.example.com'
s = requests.Session()

# Send first request to set initial cookies
s.get(url)

# Send subsequent requests using the same session
response = s.get(url)
print(response.content)

In this example, we create a Session object using the requests.Session() function. We then make a GET request to the URL 'https://www.example.com' using the Session object. This sets any initial cookies that are present on the website. We then make another GET request to the same URL using the same Session object. Because we are using the same Session object, any cookies received in the first response will be sent with the second request.

Conclusion

Python Requests makes it easy to work with cookies in HTTP requests and responses. You can send cookies using the headers parameter of a request or store cookies across multiple requests using a Session object. By understanding how cookies work, you can create more advanced applications that can remember user preferences and maintain user sessions.