Commit | Line | Data |
---|---|---|
920dae64 AT |
1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN"> |
2 | <html> | |
3 | <head> | |
4 | <link rel="STYLESHEET" href="lib.css" type='text/css' /> | |
5 | <link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" /> | |
6 | <link rel='start' href='../index.html' title='Python Documentation Index' /> | |
7 | <link rel="first" href="lib.html" title='Python Library Reference' /> | |
8 | <link rel='contents' href='contents.html' title="Contents" /> | |
9 | <link rel='index' href='genindex.html' title='Index' /> | |
10 | <link rel='last' href='about.html' title='About this document...' /> | |
11 | <link rel='help' href='about.html' title='About this document...' /> | |
12 | <link rel="next" href="file-cookie-jar-classes.html" /> | |
13 | <link rel="prev" href="module-cookielib.html" /> | |
14 | <link rel="parent" href="module-cookielib.html" /> | |
15 | <link rel="next" href="file-cookie-jar-classes.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>11.20.1 CookieJar and FileCookieJar Objects </title> | |
18 | </head> | |
19 | <body> | |
20 | <DIV CLASS="navigation"> | |
21 | <div id='top-navigation-panel' xml:id='top-navigation-panel'> | |
22 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
23 | <tr> | |
24 | <td class='online-navigation'><a rel="prev" title="11.20 cookielib " | |
25 | href="module-cookielib.html"><img src='../icons/previous.png' | |
26 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
27 | <td class='online-navigation'><a rel="parent" title="11.20 cookielib " | |
28 | href="module-cookielib.html"><img src='../icons/up.png' | |
29 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
30 | <td class='online-navigation'><a rel="next" title="11.20.2 FileCookieJar subclasses and" | |
31 | href="file-cookie-jar-classes.html"><img src='../icons/next.png' | |
32 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
33 | <td align="center" width="100%">Python Library Reference</td> | |
34 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
35 | href="contents.html"><img src='../icons/contents.png' | |
36 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
37 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
38 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
39 | <td class='online-navigation'><a rel="index" title="Index" | |
40 | href="genindex.html"><img src='../icons/index.png' | |
41 | border='0' height='32' alt='Index' width='32' /></A></td> | |
42 | </tr></table> | |
43 | <div class='online-navigation'> | |
44 | <b class="navlabel">Previous:</b> | |
45 | <a class="sectref" rel="prev" href="module-cookielib.html">11.20 cookielib </A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="module-cookielib.html">11.20 cookielib </A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="file-cookie-jar-classes.html">11.20.2 FileCookieJar subclasses and</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION00132010000000000000000"></A><A NAME="cookie-jar-objects"></A> | |
56 | <BR> | |
57 | 11.20.1 CookieJar and FileCookieJar Objects | |
58 | </H2> | |
59 | ||
60 | <P> | |
61 | <tt class="class">CookieJar</tt> objects support the iterator protocol for iterating | |
62 | over contained <tt class="class">Cookie</tt> objects. | |
63 | ||
64 | <P> | |
65 | <tt class="class">CookieJar</tt> has the following methods: | |
66 | ||
67 | <P> | |
68 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
69 | <td><nobr><b><tt id='l2h-3621' xml:id='l2h-3621' class="method">add_cookie_header</tt></b>(</nobr></td> | |
70 | <td><var>request</var>)</td></tr></table></dt> | |
71 | <dd> | |
72 | Add correct <span class="mailheader">Cookie:</span> header to <var>request</var>. | |
73 | ||
74 | <P> | |
75 | If policy allows (ie. the <tt class="member">rfc2965</tt> and <tt class="member">hide_cookie2</tt> | |
76 | attributes of the <tt class="class">CookieJar</tt>'s <tt class="class">CookiePolicy</tt> instance | |
77 | are true and false respectively), the <span class="mailheader">Cookie2:</span> header is | |
78 | also added when appropriate. | |
79 | ||
80 | <P> | |
81 | The <var>request</var> object (usually a <tt class="class">urllib2.Request</tt> instance) | |
82 | must support the methods <tt class="method">get_full_url()</tt>, <tt class="method">get_host()</tt>, | |
83 | <tt class="method">get_type()</tt>, <tt class="method">unverifiable()</tt>, | |
84 | <tt class="method">get_origin_req_host()</tt>, <tt class="method">has_header()</tt>, | |
85 | <tt class="method">get_header()</tt>, <tt class="method">header_items()</tt>, and | |
86 | <tt class="method">add_unredirected_header()</tt>,as documented by <tt class="module">urllib2</tt>. | |
87 | </dl> | |
88 | ||
89 | <P> | |
90 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
91 | <td><nobr><b><tt id='l2h-3622' xml:id='l2h-3622' class="method">extract_cookies</tt></b>(</nobr></td> | |
92 | <td><var>response, request</var>)</td></tr></table></dt> | |
93 | <dd> | |
94 | Extract cookies from HTTP <var>response</var> and store them in the | |
95 | <tt class="class">CookieJar</tt>, where allowed by policy. | |
96 | ||
97 | <P> | |
98 | The <tt class="class">CookieJar</tt> will look for allowable <span class="mailheader">Set-Cookie:</span> | |
99 | and <span class="mailheader">Set-Cookie2:</span> headers in the <var>response</var> argument, | |
100 | and store cookies as appropriate (subject to the | |
101 | <tt class="method">CookiePolicy.set_ok()</tt> method's approval). | |
102 | ||
103 | <P> | |
104 | The <var>response</var> object (usually the result of a call to | |
105 | <tt class="method">urllib2.urlopen()</tt>, or similar) should support an | |
106 | <tt class="method">info()</tt> method, which returns an object with a | |
107 | <tt class="method">getallmatchingheaders()</tt> method (usually a | |
108 | <tt class="class">mimetools.Message</tt> instance). | |
109 | ||
110 | <P> | |
111 | The <var>request</var> object (usually a <tt class="class">urllib2.Request</tt> instance) | |
112 | must support the methods <tt class="method">get_full_url()</tt>, <tt class="method">get_host()</tt>, | |
113 | <tt class="method">unverifiable()</tt>, and <tt class="method">get_origin_req_host()</tt>, as | |
114 | documented by <tt class="module">urllib2</tt>. The request is used to set default | |
115 | values for cookie-attributes as well as for checking that the cookie | |
116 | is allowed to be set. | |
117 | </dl> | |
118 | ||
119 | <P> | |
120 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
121 | <td><nobr><b><tt id='l2h-3623' xml:id='l2h-3623' class="method">set_policy</tt></b>(</nobr></td> | |
122 | <td><var>policy</var>)</td></tr></table></dt> | |
123 | <dd> | |
124 | Set the <tt class="class">CookiePolicy</tt> instance to be used. | |
125 | </dl> | |
126 | ||
127 | <P> | |
128 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
129 | <td><nobr><b><tt id='l2h-3624' xml:id='l2h-3624' class="method">make_cookies</tt></b>(</nobr></td> | |
130 | <td><var>response, request</var>)</td></tr></table></dt> | |
131 | <dd> | |
132 | Return sequence of <tt class="class">Cookie</tt> objects extracted from | |
133 | <var>response</var> object. | |
134 | ||
135 | <P> | |
136 | See the documentation for <tt class="method">extract_cookies</tt> for the interfaces | |
137 | required of the <var>response</var> and <var>request</var> arguments. | |
138 | </dl> | |
139 | ||
140 | <P> | |
141 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
142 | <td><nobr><b><tt id='l2h-3625' xml:id='l2h-3625' class="method">set_cookie_if_ok</tt></b>(</nobr></td> | |
143 | <td><var>cookie, request</var>)</td></tr></table></dt> | |
144 | <dd> | |
145 | Set a <tt class="class">Cookie</tt> if policy says it's OK to do so. | |
146 | </dl> | |
147 | ||
148 | <P> | |
149 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
150 | <td><nobr><b><tt id='l2h-3626' xml:id='l2h-3626' class="method">set_cookie</tt></b>(</nobr></td> | |
151 | <td><var>cookie</var>)</td></tr></table></dt> | |
152 | <dd> | |
153 | Set a <tt class="class">Cookie</tt>, without checking with policy to see whether or | |
154 | not it should be set. | |
155 | </dl> | |
156 | ||
157 | <P> | |
158 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
159 | <td><nobr><b><tt id='l2h-3627' xml:id='l2h-3627' class="method">clear</tt></b>(</nobr></td> | |
160 | <td><var></var><big>[</big><var>domain</var><big>[</big><var>, | |
161 | path</var><big>[</big><var>, name</var><big>]</big><var></var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> | |
162 | <dd> | |
163 | Clear some cookies. | |
164 | ||
165 | <P> | |
166 | If invoked without arguments, clear all cookies. If given a single | |
167 | argument, only cookies belonging to that <var>domain</var> will be removed. | |
168 | If given two arguments, cookies belonging to the specified | |
169 | <var>domain</var> and URL <var>path</var> are removed. If given three | |
170 | arguments, then the cookie with the specified <var>domain</var>, <var>path</var> | |
171 | and <var>name</var> is removed. | |
172 | ||
173 | <P> | |
174 | Raises <tt class="exception">KeyError</tt> if no matching cookie exists. | |
175 | </dl> | |
176 | ||
177 | <P> | |
178 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
179 | <td><nobr><b><tt id='l2h-3628' xml:id='l2h-3628' class="method">clear_session_cookies</tt></b>(</nobr></td> | |
180 | <td><var></var>)</td></tr></table></dt> | |
181 | <dd> | |
182 | Discard all session cookies. | |
183 | ||
184 | <P> | |
185 | Discards all contained cookies that have a true <tt class="member">discard</tt> | |
186 | attribute (usually because they had either no <code>max-age</code> or | |
187 | <code>expires</code> cookie-attribute, or an explicit <code>discard</code> | |
188 | cookie-attribute). For interactive browsers, the end of a session | |
189 | usually corresponds to closing the browser window. | |
190 | ||
191 | <P> | |
192 | Note that the <tt class="method">save()</tt> method won't save session cookies | |
193 | anyway, unless you ask otherwise by passing a true | |
194 | <var>ignore_discard</var> argument. | |
195 | </dl> | |
196 | ||
197 | <P> | |
198 | <tt class="class">FileCookieJar</tt> implements the following additional methods: | |
199 | ||
200 | <P> | |
201 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
202 | <td><nobr><b><tt id='l2h-3629' xml:id='l2h-3629' class="method">save</tt></b>(</nobr></td> | |
203 | <td><var>filename=<tt class="constant">None</tt>, | |
204 | ignore_discard=<tt class="constant">False</tt>, ignore_expires=<tt class="constant">False</tt></var>)</td></tr></table></dt> | |
205 | <dd> | |
206 | Save cookies to a file. | |
207 | ||
208 | <P> | |
209 | This base class raises <tt class="class">NotImplementedError</tt>. Subclasses may | |
210 | leave this method unimplemented. | |
211 | ||
212 | <P> | |
213 | <var>filename</var> is the name of file in which to save cookies. If | |
214 | <var>filename</var> is not specified, <tt class="member">self.filename</tt> is used (whose | |
215 | default is the value passed to the constructor, if any); if | |
216 | <tt class="member">self.filename</tt> is <tt class="constant">None</tt>, <tt class="exception">ValueError</tt> is | |
217 | raised. | |
218 | ||
219 | <P> | |
220 | <var>ignore_discard</var>: save even cookies set to be discarded. | |
221 | <var>ignore_expires</var>: save even cookies that have expired | |
222 | ||
223 | <P> | |
224 | The file is overwritten if it already exists, thus wiping all the | |
225 | cookies it contains. Saved cookies can be restored later using the | |
226 | <tt class="method">load()</tt> or <tt class="method">revert()</tt> methods. | |
227 | </dl> | |
228 | ||
229 | <P> | |
230 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
231 | <td><nobr><b><tt id='l2h-3630' xml:id='l2h-3630' class="method">load</tt></b>(</nobr></td> | |
232 | <td><var>filename=<tt class="constant">None</tt>, | |
233 | ignore_discard=<tt class="constant">False</tt>, ignore_expires=<tt class="constant">False</tt></var>)</td></tr></table></dt> | |
234 | <dd> | |
235 | Load cookies from a file. | |
236 | ||
237 | <P> | |
238 | Old cookies are kept unless overwritten by newly loaded ones. | |
239 | ||
240 | <P> | |
241 | Arguments are as for <tt class="method">save()</tt>. | |
242 | ||
243 | <P> | |
244 | The named file must be in the format understood by the class, or | |
245 | <tt class="exception">LoadError</tt> will be raised. | |
246 | </dl> | |
247 | ||
248 | <P> | |
249 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
250 | <td><nobr><b><tt id='l2h-3631' xml:id='l2h-3631' class="method">revert</tt></b>(</nobr></td> | |
251 | <td><var>filename=<tt class="constant">None</tt>, | |
252 | ignore_discard=<tt class="constant">False</tt>, ignore_expires=<tt class="constant">False</tt></var>)</td></tr></table></dt> | |
253 | <dd> | |
254 | Clear all cookies and reload cookies from a saved file. | |
255 | ||
256 | <P> | |
257 | Raises <tt class="exception">cookielib.LoadError</tt> or <tt class="exception">IOError</tt> if | |
258 | reversion is not successful; the object's state will not be altered if | |
259 | this happens. | |
260 | </dl> | |
261 | ||
262 | <P> | |
263 | <tt class="class">FileCookieJar</tt> instances have the following public attributes: | |
264 | ||
265 | <P> | |
266 | <dl><dt><b><tt id='l2h-3632' xml:id='l2h-3632' class="member">filename</tt></b></dt> | |
267 | <dd> | |
268 | Filename of default file in which to keep cookies. This attribute may | |
269 | be assigned to. | |
270 | </dl> | |
271 | ||
272 | <P> | |
273 | <dl><dt><b><tt id='l2h-3633' xml:id='l2h-3633' class="member">delayload</tt></b></dt> | |
274 | <dd> | |
275 | If true, load cookies lazily from disk. This attribute should not be | |
276 | assigned to. This is only a hint, since this only affects | |
277 | performance, not behaviour (unless the cookies on disk are changing). | |
278 | A <tt class="class">CookieJar</tt> object may ignore it. None of the | |
279 | <tt class="class">FileCookieJar</tt> classes included in the standard library lazily | |
280 | loads cookies. | |
281 | </dl> | |
282 | ||
283 | <P> | |
284 | ||
285 | <DIV CLASS="navigation"> | |
286 | <div class='online-navigation'> | |
287 | <p></p><hr /> | |
288 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
289 | <tr> | |
290 | <td class='online-navigation'><a rel="prev" title="11.20 cookielib " | |
291 | href="module-cookielib.html"><img src='../icons/previous.png' | |
292 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
293 | <td class='online-navigation'><a rel="parent" title="11.20 cookielib " | |
294 | href="module-cookielib.html"><img src='../icons/up.png' | |
295 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
296 | <td class='online-navigation'><a rel="next" title="11.20.2 FileCookieJar subclasses and" | |
297 | href="file-cookie-jar-classes.html"><img src='../icons/next.png' | |
298 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
299 | <td align="center" width="100%">Python Library Reference</td> | |
300 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
301 | href="contents.html"><img src='../icons/contents.png' | |
302 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
303 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
304 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
305 | <td class='online-navigation'><a rel="index" title="Index" | |
306 | href="genindex.html"><img src='../icons/index.png' | |
307 | border='0' height='32' alt='Index' width='32' /></A></td> | |
308 | </tr></table> | |
309 | <div class='online-navigation'> | |
310 | <b class="navlabel">Previous:</b> | |
311 | <a class="sectref" rel="prev" href="module-cookielib.html">11.20 cookielib </A> | |
312 | <b class="navlabel">Up:</b> | |
313 | <a class="sectref" rel="parent" href="module-cookielib.html">11.20 cookielib </A> | |
314 | <b class="navlabel">Next:</b> | |
315 | <a class="sectref" rel="next" href="file-cookie-jar-classes.html">11.20.2 FileCookieJar subclasses and</A> | |
316 | </div> | |
317 | </div> | |
318 | <hr /> | |
319 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
320 | </DIV> | |
321 | <!--End of Navigation Panel--> | |
322 | <ADDRESS> | |
323 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
324 | </ADDRESS> | |
325 | </BODY> | |
326 | </HTML> |