Back to index

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

List of all members.

Public Member Functions

def __init__
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 Attributes

 _cookies

Detailed Description

CookieJar that can be loaded from and saved to a file.

Definition at line 1737 of file cookiejar.py.


Constructor & Destructor Documentation

def http.cookiejar.FileCookieJar.__init__ (   self,
  filename = None,
  delayload = False,
  policy = None 
)
Cookies are NOT loaded from the named file until either the .load() or
.revert() method is called.

Definition at line 1740 of file cookiejar.py.

01740 
01741     def __init__(self, filename=None, delayload=False, policy=None):
01742         """
01743         Cookies are NOT loaded from the named file until either the .load() or
01744         .revert() method is called.
01745 
01746         """
01747         CookieJar.__init__(self, policy)
01748         if filename is not None:
01749             try:
01750                 filename+""
01751             except:
01752                 raise ValueError("filename must be string-like")
01753         self.filename = filename
01754         self.delayload = bool(delayload)

Here is the caller graph for this function:


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

Reimplemented in http.cookiejar.MozillaCookieJar, and http.cookiejar.LWPCookieJar.

Definition at line 1755 of file cookiejar.py.

01755 
01756     def save(self, filename=None, ignore_discard=False, ignore_expires=False):
01757         """Save cookies to a file."""
01758         raise NotImplementedError()

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

Reimplemented from http.cookiejar.CookieJar.

Definition at line 1787 of file cookiejar.py.

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: