Back to index

python3.2  3.2.2
Public Member Functions | Public Attributes | Static Public Attributes | Private Member Functions
http.cookiejar.MozillaCookieJar Class Reference
Inheritance diagram for http.cookiejar.MozillaCookieJar:
Inheritance graph
[legend]
Collaboration diagram for http.cookiejar.MozillaCookieJar:
Collaboration graph
[legend]

List of all members.

Public Member Functions

def save
def load
def revert
def set_policy
def add_cookie_header
def make_cookies
def set_cookie_if_ok
def set_cookie
def extract_cookies
def clear
def clear_session_cookies
def clear_expired_cookies
def __iter__
def __len__
def __repr__
def __str__

Public Attributes

 filename
 delayload

Static Public Attributes

tuple magic_re = re.compile("#( Netscape)? HTTP Cookie File")
string header
tuple non_word_re = re.compile(r"\W")
tuple quote_re = re.compile(r"([\"\\])")
tuple strict_domain_re = re.compile(r"\.?[^.]*")
tuple domain_re = re.compile(r"[^.]*")
tuple dots_re = re.compile(r"^\.+")

Private Member Functions

def _really_load

Detailed Description

WARNING: you may want to backup your browser's cookies file if you use
this class to save cookies.  I *think* it works, but there have been
bugs in the past!

This class differs from CookieJar only in the format it uses to save and
load cookies to and from a file.  This class uses the Mozilla/Netscape
`cookies.txt' format.  lynx uses this file format, too.

Don't expect cookies saved while the browser is running to be noticed by
the browser (in fact, Mozilla on unix will overwrite your saved cookies if
you change them on disk while it's running; on Windows, you probably can't
save at all while the browser is running).

Note that the Mozilla/Netscape format will downgrade RFC2965 cookies to
Netscape cookies on saving.

In particular, the cookie version and port number information is lost,
together with information about whether or not Path, Port and Discard were
specified by the Set-Cookie2 (or Set-Cookie) header, and whether or not the
domain as set in the HTTP header started with a dot (yes, I'm aware some
domains in Netscape files start with a dot and some don't -- trust me, you
really don't want to know any more about this).

Note that though Mozilla and Netscape use the same format, they use
slightly different headers.  The class saves cookies using the Netscape
header by default (Mozilla can cope with that).

Definition at line 1950 of file cookiejar.py.


Member Function Documentation

def http.cookiejar.CookieJar.__iter__ (   self) [inherited]

Definition at line 1714 of file cookiejar.py.

01714 
01715     def __iter__(self):
01716         return deepvalues(self._cookies)

Here is the call graph for this function:

def http.cookiejar.CookieJar.__len__ (   self) [inherited]
Return number of contained cookies.

Definition at line 1717 of file cookiejar.py.

01717 
01718     def __len__(self):
01719         """Return number of contained cookies."""
01720         i = 0
01721         for cookie in self: i = i + 1
01722         return i

def http.cookiejar.CookieJar.__repr__ (   self) [inherited]

Definition at line 1723 of file cookiejar.py.

01723 
01724     def __repr__(self):
01725         r = []
01726         for cookie in self: r.append(repr(cookie))
01727         return "<%s[%s]>" % (self.__class__, ", ".join(r))

def http.cookiejar.CookieJar.__str__ (   self) [inherited]

Definition at line 1728 of file cookiejar.py.

01728 
01729     def __str__(self):
01730         r = []
01731         for cookie in self: r.append(str(cookie))
01732         return "<%s[%s]>" % (self.__class__, ", ".join(r))
01733 
01734 
# derives from IOError for backwards-compatibility with Python 2.4.0
def http.cookiejar.MozillaCookieJar._really_load (   self,
  f,
  filename,
  ignore_discard,
  ignore_expires 
) [private]

Definition at line 1989 of file cookiejar.py.

01989 
01990     def _really_load(self, f, filename, ignore_discard, ignore_expires):
01991         now = time.time()
01992 
01993         magic = f.readline()
01994         if not self.magic_re.search(magic):
01995             f.close()
01996             raise LoadError(
01997                 "%r does not look like a Netscape format cookies file" %
01998                 filename)
01999 
02000         try:
02001             while 1:
02002                 line = f.readline()
02003                 if line == "": break
02004 
02005                 # last field may be absent, so keep any trailing tab
02006                 if line.endswith("\n"): line = line[:-1]
02007 
02008                 # skip comments and blank lines XXX what is $ for?
02009                 if (line.strip().startswith(("#", "$")) or
02010                     line.strip() == ""):
02011                     continue
02012 
02013                 domain, domain_specified, path, secure, expires, name, value = \
02014                         line.split("\t")
02015                 secure = (secure == "TRUE")
02016                 domain_specified = (domain_specified == "TRUE")
02017                 if name == "":
02018                     # cookies.txt regards 'Set-Cookie: foo' as a cookie
02019                     # with no name, whereas http.cookiejar regards it as a
02020                     # cookie with no value.
02021                     name = value
02022                     value = None
02023 
02024                 initial_dot = domain.startswith(".")
02025                 assert domain_specified == initial_dot
02026 
02027                 discard = False
02028                 if expires == "":
02029                     expires = None
02030                     discard = True
02031 
02032                 # assume path_specified is false
02033                 c = Cookie(0, name, value,
02034                            None, False,
02035                            domain, domain_specified, initial_dot,
02036                            path, False,
02037                            secure,
02038                            expires,
02039                            discard,
02040                            None,
02041                            None,
02042                            {})
02043                 if not ignore_discard and c.discard:
02044                     continue
02045                 if not ignore_expires and c.is_expired(now):
02046                     continue
02047                 self.set_cookie(c)
02048 
02049         except IOError:
02050             raise
02051         except Exception:
02052             _warn_unhandled_exception()
02053             raise LoadError("invalid Netscape format cookies file %r: %r" %
02054                             (filename, line))

Here is the call graph for this function:

Here is the caller graph for this function:

def http.cookiejar.CookieJar.add_cookie_header (   self,
  request 
) [inherited]
Add correct Cookie: header to request (urllib.request.Request object).

The Cookie2 header is also added unless policy.hide_cookie2 is true.

Definition at line 1316 of file cookiejar.py.

01316 
01317     def add_cookie_header(self, request):
01318         """Add correct Cookie: header to request (urllib.request.Request object).
01319 
01320         The Cookie2 header is also added unless policy.hide_cookie2 is true.
01321 
01322         """
01323         _debug("add_cookie_header")
01324         self._cookies_lock.acquire()
01325         try:
01326 
01327             self._policy._now = self._now = int(time.time())
01328 
01329             cookies = self._cookies_for_request(request)
01330 
01331             attrs = self._cookie_attrs(cookies)
01332             if attrs:
01333                 if not request.has_header("Cookie"):
01334                     request.add_unredirected_header(
01335                         "Cookie", "; ".join(attrs))
01336 
01337             # if necessary, advertise that we know RFC 2965
01338             if (self._policy.rfc2965 and not self._policy.hide_cookie2 and
01339                 not request.has_header("Cookie2")):
01340                 for cookie in cookies:
01341                     if cookie.version != 1:
01342                         request.add_unredirected_header("Cookie2", '$Version="1"')
01343                         break
01344 
01345         finally:
01346             self._cookies_lock.release()
01347 
01348         self.clear_expired_cookies()

Here is the call graph for this function:

def http.cookiejar.CookieJar.clear (   self,
  domain = None,
  path = None,
  name = None 
) [inherited]
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.

Definition at line 1653 of file cookiejar.py.

01653 
01654     def clear(self, domain=None, path=None, name=None):
01655         """Clear some cookies.
01656 
01657         Invoking this method without arguments will clear all cookies.  If
01658         given a single argument, only cookies belonging to that domain will be
01659         removed.  If given two arguments, cookies belonging to the specified
01660         path within that domain are removed.  If given three arguments, then
01661         the cookie with the specified name, path and domain is removed.
01662 
01663         Raises KeyError if no matching cookie exists.
01664 
01665         """
01666         if name is not None:
01667             if (domain is None) or (path is None):
01668                 raise ValueError(
01669                     "domain and path must be given to remove a cookie by name")
01670             del self._cookies[domain][path][name]
01671         elif path is not None:
01672             if domain is None:
01673                 raise ValueError(
01674                     "domain must be given to remove cookies by path")
01675             del self._cookies[domain][path]
01676         elif domain is not None:
01677             del self._cookies[domain]
01678         else:
01679             self._cookies = {}

Here is the caller graph for this function:

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).

Definition at line 1695 of file cookiejar.py.

01695 
01696     def clear_expired_cookies(self):
01697         """Discard all expired cookies.
01698 
01699         You probably don't need to call this method: expired cookies are never
01700         sent back to the server (provided you're using DefaultCookiePolicy),
01701         this method is called by CookieJar itself every so often, and the
01702         .save() method won't save expired cookies anyway (unless you ask
01703         otherwise by passing a true ignore_expires argument).
01704 
01705         """
01706         self._cookies_lock.acquire()
01707         try:
01708             now = time.time()
01709             for cookie in self:
01710                 if cookie.is_expired(now):
01711                     self.clear(cookie.domain, cookie.path, cookie.name)
01712         finally:
01713             self._cookies_lock.release()

Here is the call graph for this function:

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.

Definition at line 1680 of file cookiejar.py.

01680 
01681     def clear_session_cookies(self):
01682         """Discard all session cookies.
01683 
01684         Note that the .save() method won't save session cookies anyway, unless
01685         you ask otherwise by passing a true ignore_discard argument.
01686 
01687         """
01688         self._cookies_lock.acquire()
01689         try:
01690             for cookie in self:
01691                 if cookie.discard:
01692                     self.clear(cookie.domain, cookie.path, cookie.name)
01693         finally:
01694             self._cookies_lock.release()

Here is the call graph for this function:

def http.cookiejar.CookieJar.extract_cookies (   self,
  response,
  request 
) [inherited]
Extract cookies from response, where allowable given the request.

Definition at line 1639 of file cookiejar.py.

01639 
01640     def extract_cookies(self, response, request):
01641         """Extract cookies from response, where allowable given the request."""
01642         _debug("extract_cookies: %s", response.info())
01643         self._cookies_lock.acquire()
01644         try:
01645             self._policy._now = self._now = int(time.time())
01646 
01647             for cookie in self.make_cookies(response, request):
01648                 if self._policy.set_ok(cookie, request):
01649                     _debug(" setting cookie: %s", cookie)
01650                     self.set_cookie(cookie)
01651         finally:
01652             self._cookies_lock.release()

Here is the call graph for this function:

def http.cookiejar.FileCookieJar.load (   self,
  filename = None,
  ignore_discard = False,
  ignore_expires = False 
) [inherited]
Load cookies from a file.

Definition at line 1759 of file cookiejar.py.

01759 
01760     def load(self, filename=None, ignore_discard=False, ignore_expires=False):
01761         """Load cookies from a file."""
01762         if filename is None:
01763             if self.filename is not None: filename = self.filename
01764             else: raise ValueError(MISSING_FILENAME_TEXT)
01765 
01766         f = open(filename)
01767         try:
01768             self._really_load(f, filename, ignore_discard, ignore_expires)
01769         finally:
01770             f.close()

Here is the call graph for this function:

def http.cookiejar.CookieJar.make_cookies (   self,
  response,
  request 
) [inherited]
Return sequence of Cookie objects extracted from response object.

Definition at line 1559 of file cookiejar.py.

01559 
01560     def make_cookies(self, response, request):
01561         """Return sequence of Cookie objects extracted from response object."""
01562         # get cookie-attributes for RFC 2965 and Netscape protocols
01563         headers = response.info()
01564         rfc2965_hdrs = headers.get_all("Set-Cookie2", [])
01565         ns_hdrs = headers.get_all("Set-Cookie", [])
01566 
01567         rfc2965 = self._policy.rfc2965
01568         netscape = self._policy.netscape
01569 
01570         if ((not rfc2965_hdrs and not ns_hdrs) or
01571             (not ns_hdrs and not rfc2965) or
01572             (not rfc2965_hdrs and not netscape) or
01573             (not netscape and not rfc2965)):
01574             return []  # no relevant cookie headers: quick exit
01575 
01576         try:
01577             cookies = self._cookies_from_attrs_set(
01578                 split_header_words(rfc2965_hdrs), request)
01579         except Exception:
01580             _warn_unhandled_exception()
01581             cookies = []
01582 
01583         if ns_hdrs and netscape:
01584             try:
01585                 # RFC 2109 and Netscape cookies
01586                 ns_cookies = self._cookies_from_attrs_set(
01587                     parse_ns_headers(ns_hdrs), request)
01588             except Exception:
01589                 _warn_unhandled_exception()
01590                 ns_cookies = []
01591             self._process_rfc2109_cookies(ns_cookies)
01592 
01593             # Look for Netscape cookies (from Set-Cookie headers) that match
01594             # corresponding RFC 2965 cookies (from Set-Cookie2 headers).
01595             # For each match, keep the RFC 2965 cookie and ignore the Netscape
01596             # cookie (RFC 2965 section 9.1).  Actually, RFC 2109 cookies are
01597             # bundled in with the Netscape cookies for this purpose, which is
01598             # reasonable behaviour.
01599             if rfc2965:
01600                 lookup = {}
01601                 for cookie in cookies:
01602                     lookup[(cookie.domain, cookie.path, cookie.name)] = None
01603 
01604                 def no_matching_rfc2965(ns_cookie, lookup=lookup):
01605                     key = ns_cookie.domain, ns_cookie.path, ns_cookie.name
01606                     return key not in lookup
01607                 ns_cookies = filter(no_matching_rfc2965, ns_cookies)
01608 
01609             if ns_cookies:
01610                 cookies.extend(ns_cookies)
01611 
01612         return cookies

Here is the call graph for this function:

Here is the caller graph for this function:

def http.cookiejar.FileCookieJar.revert (   self,
  filename = None,
  ignore_discard = False,
  ignore_expires = False 
) [inherited]
Clear all cookies and reload cookies from a saved file.

Raises LoadError (or IOError) if reversion is not successful; the
object's state will not be altered if this happens.

Definition at line 1772 of file cookiejar.py.

01772 
01773                ignore_discard=False, ignore_expires=False):
01774         """Clear all cookies and reload cookies from a saved file.
01775 
01776         Raises LoadError (or IOError) if reversion is not successful; the
01777         object's state will not be altered if this happens.
01778 
01779         """
01780         if filename is None:
01781             if self.filename is not None: filename = self.filename
01782             else: raise ValueError(MISSING_FILENAME_TEXT)
01783 
01784         self._cookies_lock.acquire()
01785         try:
01786 
01787             old_state = copy.deepcopy(self._cookies)
01788             self._cookies = {}
01789             try:
01790                 self.load(filename, ignore_discard, ignore_expires)
01791             except (LoadError, IOError):
01792                 self._cookies = old_state
01793                 raise
01794 
01795         finally:
01796             self._cookies_lock.release()
01797 

Here is the call graph for this function:

Here is the caller graph for this function:

def http.cookiejar.MozillaCookieJar.save (   self,
  filename = None,
  ignore_discard = False,
  ignore_expires = False 
)
Save cookies to a file.

Reimplemented from http.cookiejar.FileCookieJar.

Definition at line 2055 of file cookiejar.py.

02055 
02056     def save(self, filename=None, ignore_discard=False, ignore_expires=False):
02057         if filename is None:
02058             if self.filename is not None: filename = self.filename
02059             else: raise ValueError(MISSING_FILENAME_TEXT)
02060 
02061         f = open(filename, "w")
02062         try:
02063             f.write(self.header)
02064             now = time.time()
02065             for cookie in self:
02066                 if not ignore_discard and cookie.discard:
02067                     continue
02068                 if not ignore_expires and cookie.is_expired(now):
02069                     continue
02070                 if cookie.secure: secure = "TRUE"
02071                 else: secure = "FALSE"
02072                 if cookie.domain.startswith("."): initial_dot = "TRUE"
02073                 else: initial_dot = "FALSE"
02074                 if cookie.expires is not None:
02075                     expires = str(cookie.expires)
02076                 else:
02077                     expires = ""
02078                 if cookie.value is None:
02079                     # cookies.txt regards 'Set-Cookie: foo' as a cookie
02080                     # with no name, whereas http.cookiejar regards it as a
02081                     # cookie with no value.
02082                     name = ""
02083                     value = cookie.name
02084                 else:
02085                     name = cookie.name
02086                     value = cookie.value
02087                 f.write(
02088                     "\t".join([cookie.domain, initial_dot, cookie.path,
02089                                secure, expires, name, value])+
02090                     "\n")
02091         finally:
02092             f.close()

Here is the caller graph for this function:

def http.cookiejar.CookieJar.set_cookie (   self,
  cookie 
) [inherited]
Set a cookie, without checking whether or not it should be set.

Definition at line 1626 of file cookiejar.py.

01626 
01627     def set_cookie(self, cookie):
01628         """Set a cookie, without checking whether or not it should be set."""
01629         c = self._cookies
01630         self._cookies_lock.acquire()
01631         try:
01632             if cookie.domain not in c: c[cookie.domain] = {}
01633             c2 = c[cookie.domain]
01634             if cookie.path not in c2: c2[cookie.path] = {}
01635             c3 = c2[cookie.path]
01636             c3[cookie.name] = cookie
01637         finally:
01638             self._cookies_lock.release()

Here is the caller graph for this function:

def http.cookiejar.CookieJar.set_cookie_if_ok (   self,
  cookie,
  request 
) [inherited]
Set a cookie if policy says it's OK to do so.

Definition at line 1613 of file cookiejar.py.

01613 
01614     def set_cookie_if_ok(self, cookie, request):
01615         """Set a cookie if policy says it's OK to do so."""
01616         self._cookies_lock.acquire()
01617         try:
01618             self._policy._now = self._now = int(time.time())
01619 
01620             if self._policy.set_ok(cookie, request):
01621                 self.set_cookie(cookie)
01622 
01623 
01624         finally:
01625             self._cookies_lock.release()

Here is the call graph for this function:

def http.cookiejar.CookieJar.set_policy (   self,
  policy 
) [inherited]

Definition at line 1229 of file cookiejar.py.

01229 
01230     def set_policy(self, policy):
01231         self._policy = policy


Member Data Documentation

Definition at line 1753 of file cookiejar.py.

tuple http.cookiejar.CookieJar.domain_re = re.compile(r"[^.]*") [static, inherited]

Definition at line 1216 of file cookiejar.py.

tuple http.cookiejar.CookieJar.dots_re = re.compile(r"^\.+") [static, inherited]

Definition at line 1217 of file cookiejar.py.

Definition at line 1752 of file cookiejar.py.

Initial value:
"""\
# Netscape HTTP Cookie File
# http://www.netscape.com/newsref/std/cookie_spec.html
# This is a generated file!  Do not edit.

"""

Definition at line 1982 of file cookiejar.py.

tuple http.cookiejar.MozillaCookieJar.magic_re = re.compile("#( Netscape)? HTTP Cookie File") [static]

Reimplemented from http.cookiejar.CookieJar.

Definition at line 1981 of file cookiejar.py.

tuple http.cookiejar.CookieJar.non_word_re = re.compile(r"\W") [static, inherited]

Definition at line 1213 of file cookiejar.py.

tuple http.cookiejar.CookieJar.quote_re = re.compile(r"([\"\\])") [static, inherited]

Definition at line 1214 of file cookiejar.py.

tuple http.cookiejar.CookieJar.strict_domain_re = re.compile(r"\.?[^.]*") [static, inherited]

Definition at line 1215 of file cookiejar.py.


The documentation for this class was generated from the following file: