Back to index

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

List of all members.

Public Member Functions

def as_lwp_str
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 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"^\.+")
tuple magic_re = re.compile(r"^\#LWP-Cookies-(\d+\.\d+)", re.ASCII)

Private Member Functions

def _really_load

Detailed Description

The LWPCookieJar saves a sequence of"Set-Cookie3" lines.
"Set-Cookie3" is the format used by the libwww-perl libary, not known
to be compatible with any browser, but which is easy to read and
doesn't lose information about RFC 2965 cookies.

Additional methods

as_lwp_str(ignore_discard=True, ignore_expired=True)

Definition at line 1826 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.LWPCookieJar._really_load (   self,
  f,
  filename,
  ignore_discard,
  ignore_expires 
) [private]

Definition at line 1870 of file cookiejar.py.

01870 
01871     def _really_load(self, f, filename, ignore_discard, ignore_expires):
01872         magic = f.readline()
01873         if not self.magic_re.search(magic):
01874             msg = ("%r does not look like a Set-Cookie3 (LWP) format "
01875                    "file" % filename)
01876             raise LoadError(msg)
01877 
01878         now = time.time()
01879 
01880         header = "Set-Cookie3:"
01881         boolean_attrs = ("port_spec", "path_spec", "domain_dot",
01882                          "secure", "discard")
01883         value_attrs = ("version",
01884                        "port", "path", "domain",
01885                        "expires",
01886                        "comment", "commenturl")
01887 
01888         try:
01889             while 1:
01890                 line = f.readline()
01891                 if line == "": break
01892                 if not line.startswith(header):
01893                     continue
01894                 line = line[len(header):].strip()
01895 
01896                 for data in split_header_words([line]):
01897                     name, value = data[0]
01898                     standard = {}
01899                     rest = {}
01900                     for k in boolean_attrs:
01901                         standard[k] = False
01902                     for k, v in data[1:]:
01903                         if k is not None:
01904                             lc = k.lower()
01905                         else:
01906                             lc = None
01907                         # don't lose case distinction for unknown fields
01908                         if (lc in value_attrs) or (lc in boolean_attrs):
01909                             k = lc
01910                         if k in boolean_attrs:
01911                             if v is None: v = True
01912                             standard[k] = v
01913                         elif k in value_attrs:
01914                             standard[k] = v
01915                         else:
01916                             rest[k] = v
01917 
01918                     h = standard.get
01919                     expires = h("expires")
01920                     discard = h("discard")
01921                     if expires is not None:
01922                         expires = iso2time(expires)
01923                     if expires is None:
01924                         discard = True
01925                     domain = h("domain")
01926                     domain_specified = domain.startswith(".")
01927                     c = Cookie(h("version"), name, value,
01928                                h("port"), h("port_spec"),
01929                                domain, domain_specified, h("domain_dot"),
01930                                h("path"), h("path_spec"),
01931                                h("secure"),
01932                                expires,
01933                                discard,
01934                                h("comment"),
01935                                h("commenturl"),
01936                                rest)
01937                     if not ignore_discard and c.discard:
01938                         continue
01939                     if not ignore_expires and c.is_expired(now):
01940                         continue
01941                     self.set_cookie(c)
01942 
01943         except IOError:
01944             raise
01945         except Exception:
01946             _warn_unhandled_exception()
01947             raise LoadError("invalid Set-Cookie3 format file %r: %r" %
01948                             (filename, line))
01949 

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.LWPCookieJar.as_lwp_str (   self,
  ignore_discard = True,
  ignore_expires = True 
)
Return cookies as a string of "\n"-separated "Set-Cookie3" headers.

ignore_discard and ignore_expires: see docstring for FileCookieJar.save

Definition at line 1839 of file cookiejar.py.

01839 
01840     def as_lwp_str(self, ignore_discard=True, ignore_expires=True):
01841         """Return cookies as a string of "\n"-separated "Set-Cookie3" headers.
01842 
01843         ignore_discard and ignore_expires: see docstring for FileCookieJar.save
01844 
01845         """
01846         now = time.time()
01847         r = []
01848         for cookie in self:
01849             if not ignore_discard and cookie.discard:
01850                 continue
01851             if not ignore_expires and cookie.is_expired(now):
01852                 continue
01853             r.append("Set-Cookie3: %s" % lwp_cookie_str(cookie))
01854         return "\n".join(r+[""])

Here is the call graph for this function:

Here is the caller 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.LWPCookieJar.save (   self,
  filename = None,
  ignore_discard = False,
  ignore_expires = False 
)
Save cookies to a file.

Reimplemented from http.cookiejar.FileCookieJar.

Definition at line 1855 of file cookiejar.py.

01855 
01856     def save(self, filename=None, ignore_discard=False, ignore_expires=False):
01857         if filename is None:
01858             if self.filename is not None: filename = self.filename
01859             else: raise ValueError(MISSING_FILENAME_TEXT)
01860 
01861         f = open(filename, "w")
01862         try:
01863             # There really isn't an LWP Cookies 2.0 format, but this indicates
01864             # that there is extra information in here (domain_dot and
01865             # port_spec) while still being compatible with libwww-perl, I hope.
01866             f.write("#LWP-Cookies-2.0\n")
01867             f.write(self.as_lwp_str(ignore_discard, ignore_expires))
01868         finally:
01869             f.close()

Here is the call graph for this function:

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.

tuple http.cookiejar.CookieJar.magic_re = re.compile(r"^\#LWP-Cookies-(\d+\.\d+)", re.ASCII) [static, inherited]

Reimplemented in http.cookiejar.MozillaCookieJar.

Definition at line 1219 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: