Cookie handling with requests

You can use the requests library to get cookies from the response object. With the cookies property from that object, you can access the cookies list through a request.cookies.RequestsCookieJar object:

>>> response = requests.get('http://www.github.com')
>>> print(response.cookies)
<<class 'requests.cookies.RequestsCookieJar'>
[<Cookie logged_in=no for .github.com/>,<Cookie _gh_sess=N0RTMHpreVk3cUk0N05qbFNpNncyajU3MVNZVWtINmZQVTUrdnlpQ1lnYnZ6WGdXWU1aQ0d2VXJQVk4wcE1IYWd4Nk54RHRrMWdjNXAwL0lOei9JenlPeUJOeEZkcTRTSTlwTWtveEt0ei9XOWlWZGNwTXNzbmd6Tk93ekcrem9XZDduQzdjdjZXWVJEOXpNVUFXOTZRUWRYalZFay9mWDAvc2NlREJSdEREb1hOUC9LR2prYi9ZQ3FoOHlORHY3L0VvN3Z4MXk0WWJyWklyMXZnSGJXQT09LS1lYlkzY1pWRXBnWUlncjRaaUJacGhnPT0%3D--85b693ec8eee1f743fcaa3dc382ee5048cfda6f1 for github.com/>, <Cookie has_recent_activity=1 for github.com/>]>

Alternatively, you can use the Session class, requests.Session, and observe cookies from the request and the response:

>>> import requests
>>> session = requests.Session()
>>> print(session.cookies.get_dict())
{}
>>> response = session.get('http://github.com')
>>> print(session.cookies.get_dict())
{'logged_in': 'no', '_gh_sess': 'ekM5WmVnVlZkMXBEcXY1ZkdFNXJuRnltdkRiajhGTExGRyt4NHNOQ1Eyc3VpOFh6VDgzRVFQZDh5bU1rMFhYam81Qm9hYkphRUFLeUd6SHU4VCtSVS9ybUdMT2lKaGhrMyttSTRCU09IckZvUUhkdllpWG10WFFJOGNTMVFxN1oxYWJqY20vVVVYR1BNN0pSMzl4VkRaVDFCYS94WWFFS2NwNzlzMzV6ajY0K2I5K1oyZmVLU0xJd1RjbTNvdFZnYmdXaEU5ejl6TFg0d09qTjA5Q3VaQT09LS1OWlhCOXBneUdVN1JxVFBudVc0cmRBPT0%3D--c4f11645c3b08255c4667e264197c61cb02fe289', 'has_recent_activity': '1'}

The Session object has the same interface as the Requests module, so we use its get() method in the same way as we use the requests.get() method. Now, any cookies encountered are stored in the Session object.

In this script, we are going to extract cookies from the github.com domain. You can find the following code in the get_cookies_github.py file:

#!/usr/bin/env python3
import requests

def check_httponly(c):
if 'httponly' in c._rest.keys():
return True
else:
return 'x1b[31mFalsex1b[39;49m'

cookies = []
url = 'http://www.github.com'
response = requests.get(url)

for cookie in response.cookies:
print('Name:', cookie.name)
print('Value:', cookie.value)
cookies.append(cookie.value)
if not cookie.secure:
cookie.secure = 'x1b[31mFalsex1b[39;49m'
print('HTTPOnly:', check_httponly(cookie), ' ')

print(set(cookies))

In this screenshot, we can see the execution of the previous script:

Also, we can send cookies to a server with the cookies parameter. In this example, we are using the service at http://httpbin.org/cookies to send the cookie with the admin='True' value.

You can find the following code in the send_cookies.py file:

#!/usr/bin/env python3
import requests
cookies = []
url = 'http://httpbin.org/cookies'
cookies = dict(admin='True')
cookie_req = requests.get(url, cookies=cookies)
print(cookie_req.text)
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset