cookies

Compatibility code to be able to use cookielib.CookieJar with requests.

requests.utils imports from here, so be careful with imports.

Module Contents

Classes

MockRequest(self,request) Wraps a requests.Request to mimic a urllib2.Request.
MockResponse(self,headers) Wraps a httplib.HTTPMessage to mimic a urllib.addinfourl.
CookieConflictError() There are two cookies that meet the criteria specified in the cookie jar.
RequestsCookieJar() Compatibility class; is a cookielib.CookieJar, but exposes a dict

Functions

extract_cookies_to_jar(jar,request,response) Extract the cookies from the response into a CookieJar.
get_cookie_header(jar,request) Produce an appropriate Cookie header string to be sent with request, or None.
remove_cookie_by_name(cookiejar,name,domain=None,path=None) Unsets a cookie by name, by default over all domains and paths.
_copy_cookie_jar(jar)
create_cookie(name,value,**kwargs) Make a cookie from underspecified parameters.
morsel_to_cookie(morsel) Convert a Morsel object into a Cookie containing the one k/v pair.
cookiejar_from_dict(cookie_dict,cookiejar=None,overwrite=True) Returns a CookieJar from a key/value dictionary.
merge_cookies(cookiejar,cookies) Add cookies to cookiejar and returns a merged CookieJar.
class MockRequest(request)

Wraps a requests.Request to mimic a urllib2.Request.

The code in cookielib.CookieJar expects this interface in order to correctly manage cookie policies, i.e., determine whether a cookie can be set, given the domains of the request and the cookie.

The original request object is read-only. The client is responsible for collecting the new headers via get_new_headers() and interpreting them appropriately. You probably want get_cookie_header, defined below.

__init__(request)
get_type()
get_host()
get_origin_req_host()
get_full_url()
is_unverifiable()
has_header(name)
get_header(name, default=None)
add_header(key, val)

cookielib has no legitimate use for this method; add it back if you find one.

add_unredirected_header(name, value)
get_new_headers()
unverifiable()
origin_req_host()
host()
class MockResponse(headers)

Wraps a httplib.HTTPMessage to mimic a urllib.addinfourl.

…what? Basically, expose the parsed HTTP headers from the server response the way cookielib expects to see them.

__init__(headers)

Make a MockResponse for cookielib to read.

Parameters:headers – a httplib.HTTPMessage or analogous carrying the headers
info()
getheaders(name)
extract_cookies_to_jar(jar, request, response)

Extract the cookies from the response into a CookieJar.

Parameters:
  • jar – cookielib.CookieJar (not necessarily a RequestsCookieJar)
  • request – our own requests.Request object
  • response – urllib3.HTTPResponse object

Produce an appropriate Cookie header string to be sent with request, or None.

Unsets a cookie by name, by default over all domains and paths.

Wraps CookieJar.clear(), is O(n).

class CookieConflictError

There are two cookies that meet the criteria specified in the cookie jar. Use .get and .set and include domain and path args in order to be more specific.

class RequestsCookieJar

Compatibility class; is a cookielib.CookieJar, but exposes a dict interface.

This is the CookieJar we create by default for requests and sessions that don’t specify one, since some clients may expect response.cookies and session.cookies to support dict operations.

Requests does not use the dict interface internally; it’s just for compatibility with external client code. All requests code should work out of the box with externally provided instances of CookieJar, e.g. LWPCookieJar and FileCookieJar.

Unlike a regular CookieJar, this class is pickleable.

Warning

dictionary operations that are normally O(1) may be O(n).

get(name, default=None, domain=None, path=None)

Dict-like get() that also supports optional domain and path args in order to resolve naming collisions from using one cookie jar over multiple domains.

Warning

operation is O(n), not O(1).

set(name, value, **kwargs)

Dict-like set() that also supports optional domain and path args in order to resolve naming collisions from using one cookie jar over multiple domains.

iterkeys()

Dict-like iterkeys() that returns an iterator of names of cookies from the jar. See itervalues() and iteritems().

keys()

Dict-like keys() that returns a list of names of cookies from the jar. See values() and items().

itervalues()

Dict-like itervalues() that returns an iterator of values of cookies from the jar. See iterkeys() and iteritems().

values()

Dict-like values() that returns a list of values of cookies from the jar. See keys() and items().

iteritems()

Dict-like iteritems() that returns an iterator of name-value tuples from the jar. See iterkeys() and itervalues().

items()

Dict-like items() that returns a list of name-value tuples from the jar. See keys() and values(). Allows client-code to call dict(RequestsCookieJar) and get a vanilla python dict of key value pairs.

list_domains()

Utility method to list all the domains in the jar.

list_paths()

Utility method to list all the paths in the jar.

multiple_domains()

Returns True if there are multiple domains in the jar. Returns False otherwise.

get_dict(domain=None, path=None)

Takes as an argument an optional domain and path and returns a plain old Python dict of name-value pairs of cookies that meet the requirements.

__contains__(name)
__getitem__(name)

Dict-like __getitem__() for compatibility with client code. Throws exception if there are more than one cookie with name. In that case, use the more explicit get() method instead.

Warning

operation is O(n), not O(1).

__setitem__(name, value)

Dict-like __setitem__ for compatibility with client code. Throws exception if there is already a cookie of that name in the jar. In that case, use the more explicit set() method instead.

__delitem__(name)

Deletes a cookie given a name. Wraps cookielib.CookieJar’s remove_cookie_by_name().

update(other)

Updates this jar with cookies from another CookieJar or dict-like

_find(name, domain=None, path=None)

Requests uses this method internally to get cookie values. Takes as args name and optional domain and path. Returns a cookie.value. If there are conflicting cookies, _find arbitrarily chooses one. See _find_no_duplicates if you want an exception thrown if there are conflicting cookies.

_find_no_duplicates(name, domain=None, path=None)

Both __get_item__ and get call this function: it’s never used elsewhere in Requests. Takes as args name and optional domain and path. Returns a cookie.value. Throws KeyError if cookie is not found and CookieConflictError if there are multiple cookies that match name and optionally domain and path.

__getstate__()

Unlike a normal CookieJar, this class is pickleable.

__setstate__(state)

Unlike a normal CookieJar, this class is pickleable.

copy()

Return a copy of this RequestsCookieJar.

Make a cookie from underspecified parameters.

By default, the pair of name and value will be set for the domain ‘’ and sent on every request (this is sometimes called a “supercookie”).

Convert a Morsel object into a Cookie containing the one k/v pair.

cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True)

Returns a CookieJar from a key/value dictionary.

Parameters:
  • cookie_dict – Dict of key/values to insert into CookieJar.
  • cookiejar – (optional) A cookiejar to add the cookies to.
  • overwrite – (optional) If False, will not replace cookies already in the jar with new ones.
merge_cookies(cookiejar, cookies)

Add cookies to cookiejar and returns a merged CookieJar.

Parameters:
  • cookiejar – CookieJar object to add the cookies to.
  • cookies – Dictionary or CookieJar object to be added.