Commit | Line | Data |
---|---|---|
86530b38 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="module-Cookie.html" /> | |
13 | <link rel="prev" href="module-CGIHTTPServer.html" /> | |
14 | <link rel="parent" href="internet.html" /> | |
15 | <link rel="next" href="node542.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>11.20 cookielib -- Cookie handling for HTTP clients</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.19 CGIHTTPServer " | |
25 | href="module-CGIHTTPServer.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. Internet Protocols and" | |
28 | href="internet.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.1 CookieJar and FileCookieJar" | |
31 | href="node542.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-CGIHTTPServer.html">11.19 CGIHTTPServer </A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="internet.html">11. Internet Protocols and</A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="node542.html">11.20.1 CookieJar and FileCookieJar</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H1><A NAME="SECTION00132000000000000000000"> | |
56 | 11.20 <tt class="module">cookielib</tt> -- | |
57 | Cookie handling for HTTP clients</A> | |
58 | </H1> | |
59 | ||
60 | <P> | |
61 | <A NAME="module-cookielib"></A> | |
62 | ||
63 | <P> | |
64 | ||
65 | <P> | |
66 | The <tt class="module">cookielib</tt> module defines classes for automatic handling | |
67 | of HTTP cookies. It is useful for accessing web sites that require | |
68 | small pieces of data - <i class="dfn">cookies</i> - to be set on the client | |
69 | machine by an HTTP response from a web server, and then returned to | |
70 | the server in later HTTP requests. | |
71 | ||
72 | <P> | |
73 | Both the regular Netscape cookie protocol and the protocol defined by | |
74 | <a class="rfc" id='rfcref-90488' xml:id='rfcref-90488' | |
75 | href="http://www.faqs.org/rfcs/rfc2965.html">RFC 2965</a> are handled. RFC 2965 handling is switched off by default. | |
76 | <a class="rfc" id='rfcref-90490' xml:id='rfcref-90490' | |
77 | href="http://www.faqs.org/rfcs/rfc2109.html">RFC 2109</a> cookies are parsed as Netscape cookies and subsequently | |
78 | treated as RFC 2965 cookies. Note that the great majority of cookies | |
79 | on the Internet are Netscape cookies. <tt class="module">cookielib</tt> attempts to | |
80 | follow the de-facto Netscape cookie protocol (which differs | |
81 | substantially from that set out in the original Netscape | |
82 | specification), including taking note of the <code>max-age</code> and | |
83 | <code>port</code> cookie-attributes introduced with RFC 2109. <span class="note"><b class="label">Note:</b> | |
84 | The | |
85 | various named parameters found in <span class="mailheader">Set-Cookie:</span> and | |
86 | <span class="mailheader">Set-Cookie2:</span> headers (eg. <code>domain</code> and | |
87 | <code>expires</code>) are conventionally referred to as <i class="dfn">attributes</i>. | |
88 | To distinguish them from Python attributes, the documentation for this | |
89 | module uses the term <i class="dfn">cookie-attribute</i> instead</span>. | |
90 | ||
91 | <P> | |
92 | The module defines the following exception: | |
93 | ||
94 | <P> | |
95 | <dl><dt><b><span class="typelabel">exception</span> <tt id='l2h-3615' xml:id='l2h-3615' class="exception">LoadError</tt></b></dt> | |
96 | <dd> | |
97 | Instances of <tt class="class">FileCookieJar</tt> raise this exception on failure to | |
98 | load cookies from a file. | |
99 | </dd></dl> | |
100 | ||
101 | <P> | |
102 | The following classes are provided: | |
103 | ||
104 | <P> | |
105 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
106 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-3616' xml:id='l2h-3616' class="class">CookieJar</tt></b>(</nobr></td> | |
107 | <td><var>policy=<tt class="constant">None</tt></var>)</td></tr></table></dt> | |
108 | <dd> | |
109 | <var>policy</var> is an object implementing the <tt class="class">CookiePolicy</tt> | |
110 | interface. | |
111 | ||
112 | <P> | |
113 | The <tt class="class">CookieJar</tt> class stores HTTP cookies. It extracts cookies | |
114 | from HTTP requests, and returns them in HTTP responses. | |
115 | <tt class="class">CookieJar</tt> instances automatically expire contained cookies | |
116 | when necessary. Subclasses are also responsible for storing and | |
117 | retrieving cookies from a file or database. | |
118 | </dl> | |
119 | ||
120 | <P> | |
121 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
122 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-3617' xml:id='l2h-3617' class="class">FileCookieJar</tt></b>(</nobr></td> | |
123 | <td><var>filename, delayload=<tt class="constant">None</tt>, | |
124 | policy=<tt class="constant">None</tt></var>)</td></tr></table></dt> | |
125 | <dd> | |
126 | <var>policy</var> is an object implementing the <tt class="class">CookiePolicy</tt> | |
127 | interface. For the other arguments, see the documentation for the | |
128 | corresponding attributes. | |
129 | ||
130 | <P> | |
131 | A <tt class="class">CookieJar</tt> which can load cookies from, and perhaps save | |
132 | cookies to, a file on disk. Cookies are <strong>NOT</strong> loaded from the | |
133 | named file until either the <tt class="method">load()</tt> or <tt class="method">revert()</tt> | |
134 | method is called. Subclasses of this class are documented in section | |
135 | <A href="file-cookie-jar-classes.html#file-cookie-jar-classes">11.20.2</A>. | |
136 | </dl> | |
137 | ||
138 | <P> | |
139 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
140 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-3618' xml:id='l2h-3618' class="class">CookiePolicy</tt></b>(</nobr></td> | |
141 | <td><var></var>)</td></tr></table></dt> | |
142 | <dd> | |
143 | This class is responsible for deciding whether each cookie should be | |
144 | accepted from / returned to the server. | |
145 | </dl> | |
146 | ||
147 | <P> | |
148 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
149 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-3619' xml:id='l2h-3619' class="class">DefaultCookiePolicy</tt></b>(</nobr></td> | |
150 | <td><var> | |
151 | blocked_domains=<tt class="constant">None</tt>, | |
152 | allowed_domains=<tt class="constant">None</tt>, | |
153 | netscape=<tt class="constant">True</tt>, rfc2965=<tt class="constant">False</tt>, | |
154 | hide_cookie2=<tt class="constant">False</tt>, | |
155 | strict_domain=<tt class="constant">False</tt>, | |
156 | strict_rfc2965_unverifiable=<tt class="constant">True</tt>, | |
157 | strict_ns_unverifiable=<tt class="constant">False</tt>, | |
158 | strict_ns_domain=<tt class="constant">DefaultCookiePolicy.DomainLiberal</tt>, | |
159 | strict_ns_set_initial_dollar=<tt class="constant">False</tt>, | |
160 | strict_ns_set_path=<tt class="constant">False</tt> | |
161 | </var>)</td></tr></table></dt> | |
162 | <dd> | |
163 | ||
164 | <P> | |
165 | Constructor arguments should be passed as keyword arguments only. | |
166 | <var>blocked_domains</var> is a sequence of domain names that we never | |
167 | accept cookies from, nor return cookies to. <var>allowed_domains</var> if | |
168 | not <tt class="constant">None</tt>, this is a sequence of the only domains for which | |
169 | we accept and return cookies. For all other arguments, see the | |
170 | documentation for <tt class="class">CookiePolicy</tt> and <tt class="class">DefaultCookiePolicy</tt> | |
171 | objects. | |
172 | ||
173 | <P> | |
174 | <tt class="class">DefaultCookiePolicy</tt> implements the standard accept / reject | |
175 | rules for Netscape and RFC 2965 cookies. RFC 2109 cookies | |
176 | (ie. cookies received in a <span class="mailheader">Set-Cookie:</span> header with a | |
177 | version cookie-attribute of 1) are treated according to the RFC 2965 | |
178 | rules. <tt class="class">DefaultCookiePolicy</tt> also provides some parameters to | |
179 | allow some fine-tuning of policy. | |
180 | </dl> | |
181 | ||
182 | <P> | |
183 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
184 | <td><nobr><b><span class="typelabel">class</span> <tt id='l2h-3620' xml:id='l2h-3620' class="class">Cookie</tt></b>(</nobr></td> | |
185 | <td><var></var>)</td></tr></table></dt> | |
186 | <dd> | |
187 | This class represents Netscape, RFC 2109 and RFC 2965 cookies. It is | |
188 | not expected that users of <tt class="module">cookielib</tt> construct their own | |
189 | <tt class="class">Cookie</tt> instances. Instead, if necessary, call | |
190 | <tt class="method">make_cookies()</tt> on a <tt class="class">CookieJar</tt> instance. | |
191 | </dl> | |
192 | ||
193 | <P> | |
194 | <div class="seealso"> | |
195 | <p class="heading">See Also:</p> | |
196 | ||
197 | <P> | |
198 | <dl compact="compact" class="seemodule"> | |
199 | <dt>Module <b><tt class="module"><a href="module-urllib2.html">urllib2</a></tt>:</b> | |
200 | <dd>URL opening with automatic cookie handling. | |
201 | </dl> | |
202 | ||
203 | <P> | |
204 | <dl compact="compact" class="seemodule"> | |
205 | <dt>Module <b><tt class="module"><a href="module-Cookie.html">Cookie</a></tt>:</b> | |
206 | <dd>HTTP cookie classes, principally useful for | |
207 | server-side code. The <tt class="module">cookielib</tt> and <tt class="module">Cookie</tt> modules | |
208 | do not depend on each other. | |
209 | </dl> | |
210 | ||
211 | <P> | |
212 | <dl compact="compact" class="seeurl"> | |
213 | <dt><a href="http://wwwsearch.sf.net/ClientCookie/" | |
214 | class="url">http://wwwsearch.sf.net/ClientCookie/</a></dt> | |
215 | <dd>Extensions to this | |
216 | module, including a class for reading Microsoft Internet Explorer | |
217 | cookies on Windows.</dd> | |
218 | </dl> | |
219 | ||
220 | <P> | |
221 | <dl compact="compact" class="seeurl"> | |
222 | <dt><a href="http://www.netscape.com/newsref/std/cookie_spec.html" | |
223 | class="url">http://www.netscape.com/newsref/std/cookie_spec.html</a></dt> | |
224 | <dd>The | |
225 | specification of the original Netscape cookie protocol. Though this | |
226 | is still the dominant protocol, the 'Netscape cookie protocol' | |
227 | implemented by all the major browsers (and <tt class="module">cookielib</tt>) only | |
228 | bears a passing resemblance to the one sketched out in | |
229 | <code>cookie_spec.html</code>.</dd> | |
230 | </dl> | |
231 | ||
232 | <P> | |
233 | <dl compact="compact" class="seerfc"> | |
234 | <dt><a href="http://www.faqs.org/rfcs/rfc2109.html" | |
235 | title="HTTP State Management Mechanism" | |
236 | >RFC 2109, <em>HTTP State Management Mechanism</em></a> | |
237 | <dd>Obsoleted by RFC 2965. | |
238 | Uses <span class="mailheader">Set-Cookie:</span> with version=1. | |
239 | </dl> | |
240 | ||
241 | <P> | |
242 | <dl compact="compact" class="seerfc"> | |
243 | <dt><a href="http://www.faqs.org/rfcs/rfc2965.html" | |
244 | title="HTTP State Management Mechanism" | |
245 | >RFC 2965, <em>HTTP State Management Mechanism</em></a> | |
246 | <dd>The Netscape protocol | |
247 | with the bugs fixed. Uses <span class="mailheader">Set-Cookie2:</span> in place of | |
248 | <span class="mailheader">Set-Cookie:</span>. Not widely used. | |
249 | </dl> | |
250 | ||
251 | <P> | |
252 | <dl compact="compact" class="seeurl"> | |
253 | <dt><a href="http://kristol.org/cookie/errata.html" | |
254 | class="url">http://kristol.org/cookie/errata.html</a></dt> | |
255 | <dd>Unfinished errata to | |
256 | RFC 2965.</dd> | |
257 | </dl> | |
258 | ||
259 | <P> | |
260 | <dl compact="compact" class="seerfc"> | |
261 | <dt><a href="http://www.faqs.org/rfcs/rfc2964.html" | |
262 | title="Use of HTTP State Management" | |
263 | >RFC 2964, <em>Use of HTTP State Management</em></a> | |
264 | <dd> | |
265 | </dl> | |
266 | ||
267 | <P> | |
268 | </div> | |
269 | ||
270 | <P> | |
271 | ||
272 | <p><br /></p><hr class='online-navigation' /> | |
273 | <div class='online-navigation'> | |
274 | <!--Table of Child-Links--> | |
275 | <A NAME="CHILD_LINKS"><STRONG>Subsections</STRONG></a> | |
276 | ||
277 | <UL CLASS="ChildLinks"> | |
278 | <LI><A href="node542.html">11.20.1 CookieJar and FileCookieJar Objects</a> | |
279 | <LI><A href="file-cookie-jar-classes.html">11.20.2 FileCookieJar subclasses and co-operation with web browsers</a> | |
280 | <LI><A href="cookie-policy-objects.html">11.20.3 CookiePolicy Objects</a> | |
281 | <LI><A href="default-cookie-policy-objects.html">11.20.4 DefaultCookiePolicy Objects</a> | |
282 | <LI><A href="cookie-jar-objects.html">11.20.5 Cookie Objects</a> | |
283 | <LI><A href="cookielib-examples.html">11.20.6 Examples</a> | |
284 | </ul> | |
285 | <!--End of Table of Child-Links--> | |
286 | </div> | |
287 | ||
288 | <DIV CLASS="navigation"> | |
289 | <div class='online-navigation'> | |
290 | <p></p><hr /> | |
291 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
292 | <tr> | |
293 | <td class='online-navigation'><a rel="prev" title="11.19 CGIHTTPServer " | |
294 | href="module-CGIHTTPServer.html"><img src='../icons/previous.png' | |
295 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
296 | <td class='online-navigation'><a rel="parent" title="11. Internet Protocols and" | |
297 | href="internet.html"><img src='../icons/up.png' | |
298 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
299 | <td class='online-navigation'><a rel="next" title="11.20.1 CookieJar and FileCookieJar" | |
300 | href="node542.html"><img src='../icons/next.png' | |
301 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
302 | <td align="center" width="100%">Python Library Reference</td> | |
303 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
304 | href="contents.html"><img src='../icons/contents.png' | |
305 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
306 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
307 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
308 | <td class='online-navigation'><a rel="index" title="Index" | |
309 | href="genindex.html"><img src='../icons/index.png' | |
310 | border='0' height='32' alt='Index' width='32' /></A></td> | |
311 | </tr></table> | |
312 | <div class='online-navigation'> | |
313 | <b class="navlabel">Previous:</b> | |
314 | <a class="sectref" rel="prev" href="module-CGIHTTPServer.html">11.19 CGIHTTPServer </A> | |
315 | <b class="navlabel">Up:</b> | |
316 | <a class="sectref" rel="parent" href="internet.html">11. Internet Protocols and</A> | |
317 | <b class="navlabel">Next:</b> | |
318 | <a class="sectref" rel="next" href="node542.html">11.20.1 CookieJar and FileCookieJar</A> | |
319 | </div> | |
320 | </div> | |
321 | <hr /> | |
322 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
323 | </DIV> | |
324 | <!--End of Navigation Panel--> | |
325 | <ADDRESS> | |
326 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
327 | </ADDRESS> | |
328 | </BODY> | |
329 | </HTML> |