FileCookieJar that reads from the Windows MSIE cookies database.
MSIECookieJar can read the cookie files of Microsoft Internet Explorer (MSIE) for Windows version 5 on Windows NT and version 6 on Windows XP and Windows 98. Other configurations may also work, but are untested. Saving cookies in MSIE format is NOT supported. If you save cookies, they'll be in the usual Set-Cookie3 format, which you can read back in using an instance of the plain old CookieJar class. Don't save using the same filename that you loaded cookies from, because you may succeed in clobbering your MSIE cookies index file!
You should be able to have LWP share Internet Explorer's cookies like this (note you need to supply a username to load_from_registry if you're on Windows 9x or Windows ME):
cj = MSIECookieJar(delayload=1) # find cookies index file in registry and load cookies from it cj.load_from_registry() opener = mechanize.build_opener(mechanize.HTTPCookieProcessor(cj)) response = opener.open("http://example.com/")
Iterating over a delayloaded MSIECookieJar instance will not cause any cookies to be read from disk. To force reading of all cookies from disk, call read_all_cookies. Note that the following methods iterate over self: clear_temporary_cookies, clear_expired_cookies, __len__, __repr__, __str__ and as_string.
Additional methods:
load_from_registry(ignore_discard=False, ignore_expires=False, username=None) load_cookie_data(filename, ignore_discard=False, ignore_expires=False) read_all_cookies()
There are no implemented interfaces.
cookie_re
(type: SRE_Pattern
)
<_sre.SRE_Pattern object at 0x415c2d40>
domain_re
(type: SRE_Pattern
)
<_sre.SRE_Pattern object at 0x41696360>
dots_re
(type: SRE_Pattern
)
<_sre.SRE_Pattern object at 0x41695480>
magic_re
(type: SRE_Pattern
)
<_sre.SRE_Pattern object at 0x895a600>
msie_domain_re
(type: SRE_Pattern
)
<_sre.SRE_Pattern object at 0x41685b10>
non_word_re
(type: SRE_Pattern
)
<_sre.SRE_Pattern object at 0x41690608>
padding
(type:
str
)
'\r\xf0\xad\x0b'
quote_re
(type: SRE_Pattern
)
<_sre.SRE_Pattern object at 0x416953e0>
reg_key
(type:
str
)
'Cookies'
reg_path
(type:
str
)
'software\\microsoft\\windows\\currentversion\\explorer\\shell folders'
strict_domain_re
(type: SRE_Pattern
)
<_sre.SRE_Pattern object at 0x41695430>
add_cookie_header(request)
Add correct Cookie: header to request (urllib2.Request object).
The Cookie2 header is also added unless policy.hide_cookie2 is true.
The request object (usually a urllib2.Request instance) must support the methods get_full_url, get_host, get_type, has_header, get_header, header_items and add_unredirected_header, as documented by urllib2, and the port attribute (the port number). Actually, RequestUpgradeProcessor will automatically upgrade your Request object to one with has_header, get_header, header_items and add_unredirected_header, if it lacks those methods, for compatibility with pre-2.4 versions of urllib2.
clear(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.
clear_expired_cookies()
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).
clear_session_cookies()
Discard all session cookies.
Discards all cookies held by object which had either no Max-Age or Expires cookie-attribute or an explicit Discard cookie-attribute, or which otherwise have ended up with a true discard attribute. For interactive browsers, the end of a session usually corresponds to closing the browser window.
Note that the save method won't save session cookies anyway, unless you ask otherwise by passing a true ignore_discard argument.
extract_cookies(response, request)
Extract cookies from response, where allowable given the request.
Look for allowable Set-Cookie: and Set-Cookie2: headers in the response object passed as argument. Any of these headers that are found are used to update the state of the object (subject to the policy.set_ok method's approval).
The response object (usually be the result of a call to
mechanize.urlopen, or similar) should support an info method, which
returns a mimetools.Message object (in fact, the mimetools.Message
object
may be any object that provides a getallmatchingheaders
method).
The request object (usually a urllib2.Request instance) must support the methods get_full_url and get_host, as documented by urllib2, and the port attribute (the port number). The request is used to set default values for cookie-attributes as well as for checking that the cookie is OK to be set.
load(filename, ignore_discard=False, ignore_expires=False, username=None)
Load cookies from an MSIE index.dat
cookies index file.
filename: full path to cookie index file username: only required on win9x
load_cookie_data(filename, ignore_discard=False, ignore_expires=False)
Load cookies from file containing actual cookie data.
Old cookies are kept unless overwritten by newly loaded ones.
You should not call this method if the delayload attribute is set.
I think each of these files contain all cookies for one user, domain, and path.
load_from_registry(ignore_discard=False, ignore_expires=False, username=None)
username: only required on win9x
make_cookies(response, request)
Return sequence of Cookie objects extracted from response object.
See extract_cookies.__doc__ for the interfaces required of the response and request arguments.
read_all_cookies()
Eagerly read in all cookies.
revert(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.
save(filename=None, ignore_discard=False, ignore_expires=False)
Save cookies to a file.
filename: name of file in which to save cookies ignore_discard: save even cookies set to be discarded ignore_expires: save even cookies that have expired
The file is overwritten if it already exists, thus wiping all its cookies. Saved cookies can be restored later using the load or revert methods. If filename is not specified, self.filename is used; if self.filename is None, ValueError is raised.
set_cookie(cookie)
set_cookie_if_ok(cookie, request)
Set a cookie if policy says it's OK to do so.
cookie: mechanize.Cookie instance request: see extract_cookies.__doc__ for the required interface
set_policy(policy)
There are no known subclasses.