Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Clear cookies from Requests Python

Tags:

I created variable: s = requests.session()

how to clear all cookies in this variable?

like image 901
user3537765 Avatar asked May 22 '14 20:05

user3537765


People also ask

How do you delete a session request in Python?

The delete() method sends a DELETE request to the specified url.

What is cookiejar in Python?

cookiejar module defines classes for automatic handling of HTTP cookies. It is useful for accessing web sites that require small pieces of data – cookies – to be set on the client machine by an HTTP response from a web server, and then returned to the server in later HTTP requests.


1 Answers

The Session.cookies object implements the full mutable mapping interface, so you can call:

s.cookies.clear()

to clear all the cookies.

Demo:

>>> import requests
>>> s = requests.session()
>>> s.get('http://httpbin.org/cookies/set', params={'foo': 'bar'})
<Response [200]>
>>> s.cookies.keys()
['foo']
>>> s.get('http://httpbin.org/cookies').json()
{u'cookies': {u'foo': u'bar'}}
>>> s.cookies.clear()
>>> s.cookies.keys()
[]
>>> s.get('http://httpbin.org/cookies').json()
{u'cookies': {}}

Easiest however, is just to create a new session:

s = requests.session()
like image 153
Martijn Pieters Avatar answered Sep 30 '22 07:09

Martijn Pieters