How To Get / Set Http Headers, Cookies And Manage Sessions Use Python Requests Module

In previous article How To Use Python Requests Module To Send Get Or Post Request Example, we have learned how to install and use python requests module to send http get and post request to web server. It also tell you how to post form data or pass query string parameters use python requests module also. This article will tell you how to use python requests module to manage http headers, cookies and session.

1. Get / Set Http Headers Use Python Requests Module.

1.1 Get Server Response Http Headers.

Python requests module’s headers property is used to get http headers. The headers property is a dictionary type object, you should provide the header name to get header value.

>>> import requests

>>> response = requests.get("")

>>> response.headers['content-type']
'text/html; charset=UTF-8'

If you want to get all headers, just call response.headers, it will list out all http response headers in JSON format string.

>>> response.headers

{'Date': 'Mon, ...... 'Content-Encoding': 'gzip'}

1.2 Add Custom Headers To Http Request.

import requests

# Create a python dictionary object to save custom http headers. 
custom_header = {'user-agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/74.0.3729.169 Safari/537.36'}

# Pass above custom_header dictionary object to requests module's get function's headers parameter.
response = requests.get('', headers=custom_header)

2. Get / Set Cookies Use Python Requests Module.

2.1 Get Http Cookies.

  1. Get all http response cookies by invoke response.cookies property. This property is an instance of requests.cookies.RequestsCookieJar class.
    >>> import requests
    >>> response = requests.get('')
    >>> response.cookies
  2. You can loop to print out each cookie’s data such as domain, name and cookie value like below.
    >>> for cookie in response.cookies:
    ...     print('cookie domain = ' + cookie.domain)
    ...     print('cookie name = ' +
    ...     print('cookie value = ' + cookie.value)
    ...     print('*************************************')
    cookie domain =
    cookie name = __cfduid
    cookie value = d8dccd301af7c19f3286442254fe10aa51560824472
    cookie domain =
    cookie name = active_template::80464
    cookie value = pub_site.1560824472
  3. The RequestsCookieJar class also provide methods such as items(), iteritems(), iterkeys(), itervalues(), keys(), values() to loop all cookie’s key or values in response ( each cookie has a key and a value. ).
    >>> for item in rsponse.cookies.items():
    ...     print(item)
    ('__cfduid', 'd0c1cc559af0893eb31cd23bb9bc22a261560866202')
    ('active_template::80464', 'pub_site.1560866203')
    ('ezCMPCCS', 'true')
    ('ezepvv', '92')
    ('ezoab_80464', 'mod31')
    ('ezoadgid_80464', '-1')
    ('ezopvc_80464', '1')
    ('ezoref_80464', '')
    ('ezovid_80464', '1774096901')
    ('ezovuuid_80464', '2d40f1eb-b006-4540-75cb-09a37a3b8387')
    ('ezovuuidtime_80464', '1560866206')
    ('lp_80464', '')
    >>> for item in cookies.iterkeys():
    ...     print(item)
  4. Get cookie value by cookie name.
    # Get cookie value by provided cookie name.
    >>> response.cookies['ezCMPCCS']
  5. Get all cookies by domain.
    # Use get_dict function to get related domain's cookies.
    >>> response.cookies.get_dict('')
    {'__cfduid': 'd8dccd301af7c19f3286442254fe10aa51560824472', 'active_template::80464': 'pub_site.1560824472', 'ezCMPCCS': 'true', 'ezepvv': '4265', 'ezoab_80464': 'mod40', 'ezoadgid_80464': '-2', 'ezopvc_80464': '1', 'ezoref_80464': '', 'ezovid_80464': '1493552757', 'ezovuuid_80464': 'd073b9a0-ff53-454e-7f7b-877b4d21718e', 'ezovuuidtime_80464': '1560824473', 'lp_80464': ''}
  6. List response cookie domain.
    >>> response.cookies.list_domains()

2.2 Send Http Cookies To Web Server.

  1. Send cookie use dictionary object.
    # Import python requests module.
    >>> import requests
    # Set url value.
    >>> url = ''
    # Create a dictionary object.
    >>> cookies = dict(name='jerry', password='888')
    # Use python requests module to get related url and send cookies to it with cookies parameter. 
    >>> response = requests.get(url, cookies=cookies)
  2. Send cookie use RequestsCookieJar object.
    >>> import requests
    >>> url = ''
    # Create a RequestsCookieJar object.
    >>> cookies_jar = requests.cookies.RequestsCookieJar()
    # Add first cookie, the parameters are cookie_key, cookie_value, cookie_domain, cookie_path.
    >>> cookies_jar.set('name', 'jerry', domain='', path='/cookies')
    Cookie(version=0, name='name', value='jerry', port=None, port_specified=False, domain='', domain_specified=True, domain_initial_dot=False, path='/cookies', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)
    # Add second cookie.
    >>> cookies_jar.set('password', 'jerry888', domain='', path='/cookies')
    Cookie(version=0, name='password', value='jerry888', port=None, port_specified=False, domain='', domain_specified=True, domain_initial_dot=False, path='/cookies', path_specified=True, secure=False, expires=None, discard=True, comment=None, comment_url=None, rest={'HttpOnly': None}, rfc2109=False)
    # Get url with cookie parameter.
    >>> response = requests.get(url, cookies=cookies_jar)

3. Use Session In Python Requests Module.

Python requests module’s Session() method will return a request.sessions.Session object, then all the later operates ( such as get related url page ) on this session object will use one same session.

>>> import requests
# Call requests module's session() method to return a requests.sessions.Session object.
>>> session = requests.session()
>>> print(session)
<requests.sessions.Session object at 0x1025f8e10>

The returned request.sessions.Session object provide a lot of attributes and method for you to access such as web page by url, headers, cookies value in the same session. So you can use above session object like below.

# Show all headers and cookies in this session.
>>> session.headers
{'User-Agent': 'python-requests/2.22.0', 'Accept-Encoding': 'gzip, deflate', 'Accept': '*/*', 'Connection': 'keep-alive'}
>>> session.cookies
# Use this session object to get a web page by url.
>>> response = session.get('')
# When above browse web page process complete, this session has cookies.
>>> session.cookies

# You can add custom headers for this session when get web page by url.
>>> custom_header = {'hello': 'hello_888'}

# Get web page url and send custom headers.
>>> response = session.get('', headers=custom_header)

Below link will show you python requests module session class source code, you can read it if you need.

Leave a Comment

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.