Python send request with cookie

Last updated: Jul 2, 2023
Reading time · 4 min

banner

# Table of Contents

# Python: How to get and set Cookies when using Requests

Use the Session class to set and get cookies when using the requests module in Python.

The class creates a Session object that stores the cookies and all requests that are made handle cookies automatically.

Copied!
import requests session = requests.Session() print(session.cookies.get_dict()) # <> response = session.get('http://google.com') # print(session.cookies.get_dict())

using cookies in python requests

Make sure you have the requests module installed to be able to run the code sample.

Copied!
pip install requests # or with pip3 pip3 install requests

The Session object enables you to persist cookies across requests.

The object persists cookies across all requests that were made using the Session instance.

The Session object has all of the methods of the main requests API.

The get_dict() method returns a Python dictionary of the name-value pairs of cookies.

You can also use Sessions as context managers.

Copied!
import requests with requests.Session() as session: print(session.cookies.get_dict()) # <> response = session.get('http://google.com') # print(session.cookies.get_dict())

Make sure the code that accesses the Session object is inside the indented block.

# Accessing the path and the domain when using Cookies with requests

It is very likely, that you will also have to access the domain and the path to which the request was made when accessing the cookies.

You can use a list comprehension to construct a list of dictionaries that contain the path and domain.

Copied!
import requests session = requests.Session() print(session.cookies.get_dict()) # <> print('-' * 50) response = session.get('http://google.com') # print(session.cookies.get_dict()) print('-' * 50) result = [ 'name': c.name, 'value': c.value, 'domain': c.domain, 'path': c.path> for c in session.cookies ] # [] print(result)

get path and domain while using cookies in requests

We used a list comprehension to iterate over the RequestCookieJar object ( session.cookies ) and returned a dictionary on each iteration.

The dictionary contains the name and value of the cookie, the path and the domain.

# Sending cookies with a request

If you want to send cookies with a request, set the cookies keyword argument.

Copied!
import requests session = requests.Session() response = session.get( 'https://httpbin.org/cookies', cookies='my-cookie': 'my-value'> ) # # "cookies": # "my-cookie": "my-value" # > # > print(response.text)

send cookies with request

We set the cookies keyword argument to a dictionary of key-value pairs.

# Accessing the cookies attribute directly on the Response object

In more recent versions of the requests module, you can also access the cookies attribute directly on the Response object.

Copied!
import requests response = requests.get('http://google.com', timeout=30) # print(response.cookies.get_dict()) result = [ 'name': c.name, 'value': c.value, 'domain': c.domain, 'path': c.path> for c in response.cookies ] # [] print(result)

accessing cookies attribute directly on response object

Notice that we didn’t instantiate the Session class.

We simply accessed the cookies attribute on the Response object and called the get_dict() method on the RequestCookieJar object.

However, the management of cookies is automated when you use a Session object.

This means that you won’t have to send the cookies explicitly:

Copied!
import requests session = requests.Session() response = session.get( 'https://httpbin.org/cookies', cookies='my-cookie': 'my-value'> ) # # "cookies": # "my-cookie": "my-value" # > # > print(response.text)

Because it will be done for your automatically.

# Saving the requests cookies in a file and restoring them

You can also save the requests cookies in a file.

Copied!
import json import requests response = requests.get('http://google.com', timeout=30) with open('cookies.txt', 'w', encoding='utf-8') as f: json.dump( requests.utils.dict_from_cookiejar(response.cookies), f )

The cookies.txt file stores the following JSON string.

Copied!
"AEC": "Ad49MVEu4N64Tk1gEROw417s9FgcqdqeIeVZ8eL9m-HQldzOrLAF2HvxHQ">

Notice that we used the requests.utils.dict_from_cookiejar method to create a dictionary from the RequestCookieJar object.

We then passed the dictionary and the file object to the json.dump method.

The json.dump method serializes the supplied object as a JSON formatted stream and writes it to a file.

You can then read and restore the cookies from the file.

Copied!
import json import requests session = requests.session() with open('cookies.txt', 'r', encoding='utf-8') as f: cookies = requests.utils.cookiejar_from_dict(json.load(f)) session.cookies.update(cookies) # print(session.cookies.get_dict())

We created a brand new Session object but you can use an existing Session .

  1. We used the json.load() method to convert the contents of the file to a native Python dictionary.
  2. We used the requests.utils.cookiejar_from_dict method to create a RequestCookieJar object from the dictionary.
  3. The last step is to update the cookies of the Session object with the newly read cookies.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.

Источник

How To Handle Cookies With Python Requests Module

In interface testing, most projects have interfaces that require user login to operate, and the python requests module is often used to simulate login and post-login operations. In this article, I will show you two methods to handle login certificate cookies use python requests module.

1. Use requests.utils.dict_from_cookiejar() Method To Converts Returned Cookies Into A Dictionary.

# Import python requets module. import requests def login_return_cookie(): # The website login url. login_url = 'http://www.test-abc.com/login' # Custom request headers. headers = < "Accept": "application/json, text/javascript, */*; q=0.01" ># The login form submit data. login_data = < "username": "jerry", "password": "888888" >try: # Send a post request to login_url with login_data. response = requests.post(url=login_url, headers=headers, data=login_data) # Get server response cookies. cookies = response.cookies # Convert cookies to a dictionary object. cookie = requests.utils.dict_from_cookiejar(cookies) # Return the dictionary object. return cookie except Exception as err: # When there are exceptions, print error message. print('Retrieve cookie fail:\n'.format(err))
import requests # This function will use server returned cookie for later url request. def get_data(): # First invoke login_return_cookie() method to login to the website and get server returned cookies value. cookie = login_return_cookie() # Request below url to vote, the vote action need login cookies. vote_url = 'http://www.test-abc.com/vote' # Pass server returned cookies to the vote_url request. response = requests.get(url = vote_url, cookies=cookie) print(response.text)

2. Iterate Cookies Key-Value Pairs And Concat Them Into Cookie Format String.

# Import python requets module. import requests def login_return_cookie_string(): # The website login url. login_url = 'http://www.test-abc.com/login' # Custom request headers. headers = < "Accept": "application/json, text/javascript, */*; q=0.01" ># The login form submit data. login_data = < "username": "jerry", "password": "888888" >try: # Send a post request to login_url with login_data. response = requests.post(url=login_url, headers=headers, data=login_data) # Get server response cookies item. cookies = response.cookies.items() cookie = '' # Iterate the server returned cookies. for key, value in cookies: # Concat cookie string use the key=value; cookie format. cookie += '=;'.format(key, value) # Return the dictionary object. return cookie except Exception as err: # When there are exceptions, print error message. print('Retrieve cookie fail:\n'.format(err))
import requests def get_data(): # Get server response cookie string. cookie_string = login_return_cookie_string() # Set the cookie string in http request headers. headers = < "cookie": cookie_string ># Send get request with above custom headers(contains the cookies). response = requests.get(url=get_data_url, headers=headers) print(response.text)

Источник

Читайте также:  Reverse dictionary in python
Оцените статью