cookies

module requests.cookies in requests

Description

requests.cookies
~~~~~~~~~~~~~~~~

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

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

Classes

builtins.RuntimeError(builtins.Exception)
    CookieConflictError
builtins.object
    MockRequest
    MockResponse
collections.abc.MutableMapping(collections.abc.Mapping)
    RequestsCookieJar(http.cookiejar.CookieJar, collections.abc.MutableMapping)
http.cookiejar.CookieJar(builtins.object)
    RequestsCookieJar(http.cookiejar.CookieJar, collections.abc.MutableMapping)

CookieConflictError

class CookieConflictError(builtins.RuntimeError)
 |  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.
 |  
 |  Method resolution order:
 |      CookieConflictError
 |      builtins.RuntimeError
 |      builtins.Exception
 |      builtins.BaseException
 |      builtins.object
 |  
 |  Data descriptors defined here:
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from builtins.RuntimeError:
 |  
 |  

RuntimeError.__init__

 |  
 |  __init__(self, /, *args, **kwargs)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  ----------------------------------------------------------------------
 |  Static methods inherited from builtins.RuntimeError:
 |  
 |  

RuntimeError.__new__

 |  
 |  __new__(*args, **kwargs) from builtins.type
 |      Create and return a new object.  See help(type) for accurate signature.
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from builtins.BaseException:
 |  
 |  

BaseException.__delattr__

 |  
 |  __delattr__(self, name, /)
 |      Implement delattr(self, name).
 |  
 |  

BaseException.__getattribute__

 |  
 |  __getattribute__(self, name, /)
 |      Return getattr(self, name).
 |  
 |  

BaseException.__reduce__

 |  
 |  __reduce__(...)
 |      Helper for pickle.
 |  
 |  

BaseException.__repr__

 |  
 |  __repr__(self, /)
 |      Return repr(self).
 |  
 |  

BaseException.__setattr__

 |  
 |  __setattr__(self, name, value, /)
 |      Implement setattr(self, name, value).
 |  
 |  

BaseException.__setstate__

 |  
 |  __setstate__(...)
 |  
 |  

BaseException.__str__

 |  
 |  __str__(self, /)
 |      Return str(self).
 |  
 |  

BaseException.with_traceback

 |  
 |  with_traceback(...)
 |      Exception.with_traceback(tb) --
 |      set self.__traceback__ to tb and return self.
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from builtins.BaseException:
 |  
 |  __cause__
 |      exception cause
 |  
 |  __context__
 |      exception context
 |  
 |  __dict__
 |  
 |  __suppress_context__
 |  
 |  __traceback__
 |  
 |  args

MockRequest

class MockRequest(builtins.object)
 |  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.
 |  
 |  Methods defined here:
 |  
 |  

MockRequest.__init__

 |  
 |  __init__(self, request)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  

MockRequest.add_header

 |  
 |  add_header(self, key, val)
 |      cookielib has no legitimate use for this method; add it back if you find one.
 |  
 |  

MockRequest.add_unredirected_header

 |  
 |  add_unredirected_header(self, name, value)
 |  
 |  

MockRequest.get_full_url

 |  
 |  get_full_url(self)
 |  
 |  

MockRequest.get_header

 |  
 |  get_header(self, name, default=None)
 |  
 |  

MockRequest.get_host

 |  
 |  get_host(self)
 |  
 |  

MockRequest.get_new_headers

 |  
 |  get_new_headers(self)
 |  
 |  

MockRequest.get_origin_req_host

 |  
 |  get_origin_req_host(self)
 |  
 |  

MockRequest.get_type

 |  
 |  get_type(self)
 |  
 |  

MockRequest.has_header

 |  
 |  has_header(self, name)
 |  
 |  

MockRequest.is_unverifiable

 |  
 |  is_unverifiable(self)
 |  
 |  ----------------------------------------------------------------------
 |  Readonly properties defined here:
 |  
 |  host
 |  
 |  origin_req_host
 |  
 |  unverifiable
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

MockResponse

class MockResponse(builtins.object)
 |  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.
 |  
 |  Methods defined here:
 |  
 |  

MockResponse.__init__

 |  
 |  __init__(self, headers)
 |      Make a MockResponse for `cookielib` to read.
 |      
 |      :param headers: a httplib.HTTPMessage or analogous carrying the headers
 |  
 |  

MockResponse.getheaders

 |  
 |  getheaders(self, name)
 |  
 |  

MockResponse.info

 |  
 |  info(self)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors defined here:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)

RequestsCookieJar

class RequestsCookieJar(http.cookiejar.CookieJar, collections.abc.MutableMapping)
 |  RequestsCookieJar(policy=None)
 |  
 |  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).
 |  
 |  Method resolution order:
 |      RequestsCookieJar
 |      http.cookiejar.CookieJar
 |      collections.abc.MutableMapping
 |      collections.abc.Mapping
 |      collections.abc.Collection
 |      collections.abc.Sized
 |      collections.abc.Iterable
 |      collections.abc.Container
 |      builtins.object
 |  
 |  Methods defined here:
 |  
 |  

RequestsCookieJar.__contains__

 |  
 |  __contains__(self, name)
 |  
 |  

RequestsCookieJar.__delitem__

 |  
 |  __delitem__(self, name)
 |      Deletes a cookie given a name. Wraps ``cookielib.CookieJar``'s
 |      ``remove_cookie_by_name()``.
 |  
 |  

RequestsCookieJar.__getitem__

 |  
 |  __getitem__(self, 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).
 |  
 |  

RequestsCookieJar.__getstate__

 |  
 |  __getstate__(self)
 |      Unlike a normal CookieJar, this class is pickleable.
 |  
 |  

RequestsCookieJar.__setitem__

 |  
 |  __setitem__(self, 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.
 |  
 |  

RequestsCookieJar.__setstate__

 |  
 |  __setstate__(self, state)
 |      Unlike a normal CookieJar, this class is pickleable.
 |  
 |  

RequestsCookieJar.copy

 |  
 |  copy(self)
 |      Return a copy of this RequestsCookieJar.
 |  
 |  

RequestsCookieJar.get

 |  
 |  get(self, 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).
 |  
 |  

RequestsCookieJar.get_dict

 |  
 |  get_dict(self, 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.
 |      
 |      :rtype: dict
 |  
 |  

RequestsCookieJar.get_policy

 |  
 |  get_policy(self)
 |      Return the CookiePolicy instance used.
 |  
 |  

RequestsCookieJar.items

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

RequestsCookieJar.iteritems

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

RequestsCookieJar.iterkeys

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

RequestsCookieJar.itervalues

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

RequestsCookieJar.keys

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

RequestsCookieJar.list_domains

 |  
 |  list_domains(self)
 |      Utility method to list all the domains in the jar.
 |  
 |  

RequestsCookieJar.list_paths

 |  
 |  list_paths(self)
 |      Utility method to list all the paths in the jar.
 |  
 |  

RequestsCookieJar.multiple_domains

 |  
 |  multiple_domains(self)
 |      Returns True if there are multiple domains in the jar.
 |      Returns False otherwise.
 |      
 |      :rtype: bool
 |  
 |  

RequestsCookieJar.set

 |  
 |  set(self, 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.
 |  
 |  

RequestsCookieJar.update

 |  
 |  update(self, other)
 |      Updates this jar with cookies from another CookieJar or dict-like
 |  
 |  

RequestsCookieJar.values

 |  
 |  values(self)
 |      Dict-like values() that returns a list of values of cookies from the
 |      jar.
 |      
 |      .. seealso:: keys() and items().
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes defined here:
 |  
 |  __abstractmethods__ = frozenset()
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from http.cookiejar.CookieJar:
 |  
 |  

CookieJar.__init__

 |  
 |  __init__(self, policy=None)
 |      Initialize self.  See help(type(self)) for accurate signature.
 |  
 |  

CookieJar.__iter__

 |  
 |  __iter__(self)
 |  
 |  

CookieJar.__len__

 |  
 |  __len__(self)
 |      Return number of contained cookies.
 |  
 |  

CookieJar.__repr__

 |  
 |  __repr__(self)
 |      Return repr(self).
 |  
 |  

CookieJar.__str__

 |  
 |  __str__(self)
 |      Return str(self).
 |  
 |  

CookieJar.clear

 |  
 |  clear(self, domain=None, path=None, name=None)
 |      Clear some cookies.
 |      
 |      Invoking this method without arguments will clear all cookies.  If
 |      given a single argument, only cookies belonging to that domain will be
 |      removed.  If given two arguments, cookies belonging to the specified
 |      path within that domain are removed.  If given three arguments, then
 |      the cookie with the specified name, path and domain is removed.
 |      
 |      Raises KeyError if no matching cookie exists.
 |  
 |  

CookieJar.clear_expired_cookies

 |  
 |  clear_expired_cookies(self)
 |      Discard all expired cookies.
 |      
 |      You probably don't need to call this method: expired cookies are never
 |      sent back to the server (provided you're using DefaultCookiePolicy),
 |      this method is called by CookieJar itself every so often, and the
 |      .save() method won't save expired cookies anyway (unless you ask
 |      otherwise by passing a true ignore_expires argument).
 |  
 |  

CookieJar.clear_session_cookies

 |  
 |  clear_session_cookies(self)
 |      Discard all session cookies.
 |      
 |      Note that the .save() method won't save session cookies anyway, unless
 |      you ask otherwise by passing a true ignore_discard argument.
 |  
 |  

CookieJar.extract_cookies

 |  
 |  extract_cookies(self, response, request)
 |      Extract cookies from response, where allowable given the request.
 |  
 |  

CookieJar.make_cookies

 |  
 |  make_cookies(self, response, request)
 |      Return sequence of Cookie objects extracted from response object.
 |  
 |  

CookieJar.set_policy

 |  
 |  set_policy(self, policy)
 |  
 |  ----------------------------------------------------------------------
 |  Data descriptors inherited from http.cookiejar.CookieJar:
 |  
 |  __dict__
 |      dictionary for instance variables (if defined)
 |  
 |  __weakref__
 |      list of weak references to the object (if defined)
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes inherited from http.cookiejar.CookieJar:
 |  
 |  domain_re = re.compile('[^.]*')
 |  
 |  dots_re = re.compile('^\\.+')
 |  
 |  magic_re = re.compile('^\\#LWP-Cookies-(\\d+\\.\\d+)', re.ASCII)
 |  
 |  non_word_re = re.compile('\\W')
 |  
 |  quote_re = re.compile('([\\"\\\\])')
 |  
 |  strict_domain_re = re.compile('\\.?[^.]*')
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from collections.abc.MutableMapping:
 |  
 |  

MutableMapping.pop

 |  
 |  pop(self, key, default=<object object at 0x10df02160>)
 |      D.pop(k[,d]) -> v, remove specified key and return the corresponding value.
 |      If key is not found, d is returned if given, otherwise KeyError is raised.
 |  
 |  

MutableMapping.popitem

 |  
 |  popitem(self)
 |      D.popitem() -> (k, v), remove and return some (key, value) pair
 |      as a 2-tuple; but raise KeyError if D is empty.
 |  
 |  

MutableMapping.setdefault

 |  
 |  setdefault(self, key, default=None)
 |      D.setdefault(k[,d]) -> D.get(k,d), also set D[k]=d if k not in D
 |  
 |  ----------------------------------------------------------------------
 |  Methods inherited from collections.abc.Mapping:
 |  
 |  

Mapping.__eq__

 |  
 |  __eq__(self, other)
 |      Return self==value.
 |  
 |  ----------------------------------------------------------------------
 |  Data and other attributes inherited from collections.abc.Mapping:
 |  
 |  __hash__ = None
 |  
 |  __reversed__ = None
 |  
 |  ----------------------------------------------------------------------
 |  Class methods inherited from collections.abc.Collection:
 |  
 |  

Collection.__subclasshook__

 |  
 |  __subclasshook__(C) from abc.ABCMeta
 |      Abstract classes can override this to customize issubclass().
 |      
 |      This is invoked early on by abc.ABCMeta.__subclasscheck__().
 |      It should return True, False or NotImplemented.  If it returns
 |      NotImplemented, the normal algorithm is used.  Otherwise, it
 |      overrides the normal algorithm (and the outcome is cached).
 |  
 |  ----------------------------------------------------------------------
 |  Class methods inherited from collections.abc.Iterable:
 |  
 |  

GenericAlias

 |  
 |  __class_getitem__ = GenericAlias(...) from abc.ABCMeta
 |      Represent a PEP 585 generic type
 |      
 |      E.g. for t = list[int], t.__origin__ is list and t.__args__ is (int,).

Functions

cookiejar_from_dict

cookiejar_from_dict(cookie_dict, cookiejar=None, overwrite=True)
    Returns a CookieJar from a key/value dictionary.
    
    :param cookie_dict: Dict of key/values to insert into CookieJar.
    :param cookiejar: (optional) A cookiejar to add the cookies to.
    :param overwrite: (optional) If False, will not replace cookies
        already in the jar with new ones.
    :rtype: CookieJar

extract_cookies_to_jar

extract_cookies_to_jar(jar, request, response)
    Extract the cookies from the response into a CookieJar.
    
    :param jar: cookielib.CookieJar (not necessarily a RequestsCookieJar)
    :param request: our own requests.Request object
    :param response: urllib3.HTTPResponse object

merge_cookies

merge_cookies(cookiejar, cookies)
    Add cookies to cookiejar and returns a merged CookieJar.
    
    :param cookiejar: CookieJar object to add the cookies to.
    :param cookies: Dictionary or CookieJar object to be added.
    :rtype: CookieJar