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="http-redirect-handler.html" /> | |
13 | <link rel="prev" href="opener-director-objects.html" /> | |
14 | <link rel="parent" href="module-urllib2.html" /> | |
15 | <link rel="next" href="http-redirect-handler.html" /> | |
16 | <meta name='aesop' content='information' /> | |
17 | <title>11.5.3 BaseHandler 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.5.2 OpenerDirector Objects" | |
25 | href="opener-director-objects.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.5 urllib2 " | |
28 | href="module-urllib2.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.5.4 HTTPRedirectHandler Objects" | |
31 | href="http-redirect-handler.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="opener-director-objects.html">11.5.2 OpenerDirector Objects</A> | |
46 | <b class="navlabel">Up:</b> | |
47 | <a class="sectref" rel="parent" href="module-urllib2.html">11.5 urllib2 </A> | |
48 | <b class="navlabel">Next:</b> | |
49 | <a class="sectref" rel="next" href="http-redirect-handler.html">11.5.4 HTTPRedirectHandler Objects</A> | |
50 | </div> | |
51 | <hr /></div> | |
52 | </DIV> | |
53 | <!--End of Navigation Panel--> | |
54 | ||
55 | <H2><A NAME="SECTION0013530000000000000000"></A><A NAME="base-handler-objects"></A> | |
56 | <BR> | |
57 | 11.5.3 BaseHandler Objects | |
58 | </H2> | |
59 | ||
60 | <P> | |
61 | <tt class="class">BaseHandler</tt> objects provide a couple of methods that are | |
62 | directly useful, and others that are meant to be used by derived | |
63 | classes. These are intended for direct use: | |
64 | ||
65 | <P> | |
66 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
67 | <td><nobr><b><tt id='l2h-3264' xml:id='l2h-3264' class="method">add_parent</tt></b>(</nobr></td> | |
68 | <td><var>director</var>)</td></tr></table></dt> | |
69 | <dd> | |
70 | Add a director as parent. | |
71 | </dl> | |
72 | ||
73 | <P> | |
74 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
75 | <td><nobr><b><tt id='l2h-3265' xml:id='l2h-3265' class="method">close</tt></b>(</nobr></td> | |
76 | <td><var></var>)</td></tr></table></dt> | |
77 | <dd> | |
78 | Remove any parents. | |
79 | </dl> | |
80 | ||
81 | <P> | |
82 | The following members and methods should only be used by classes | |
83 | derived from <tt class="class">BaseHandler</tt>. <span class="note"><b class="label">Note:</b> | |
84 | The convention has been | |
85 | adopted that subclasses defining <tt class="method"><var>protocol</var>_request()</tt> or | |
86 | <tt class="method"><var>protocol</var>_response()</tt> methods are named | |
87 | <tt class="class">*Processor</tt>; all others are named <tt class="class">*Handler</tt>.</span> | |
88 | ||
89 | <P> | |
90 | <dl><dt><b><tt id='l2h-3266' xml:id='l2h-3266' class="member">parent</tt></b></dt> | |
91 | <dd> | |
92 | A valid <tt class="class">OpenerDirector</tt>, which can be used to open using a | |
93 | different protocol, or handle errors. | |
94 | </dl> | |
95 | ||
96 | <P> | |
97 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
98 | <td><nobr><b><tt id='l2h-3267' xml:id='l2h-3267' class="method">default_open</tt></b>(</nobr></td> | |
99 | <td><var>req</var>)</td></tr></table></dt> | |
100 | <dd> | |
101 | This method is <em>not</em> defined in <tt class="class">BaseHandler</tt>, but | |
102 | subclasses should define it if they want to catch all URLs. | |
103 | ||
104 | <P> | |
105 | This method, if implemented, will be called by the parent | |
106 | <tt class="class">OpenerDirector</tt>. It should return a file-like object as | |
107 | described in the return value of the <tt class="method">open()</tt> of | |
108 | <tt class="class">OpenerDirector</tt>, or <code>None</code>. It should raise | |
109 | <tt class="exception">URLError</tt>, unless a truly exceptional thing happens (for | |
110 | example, <tt class="exception">MemoryError</tt> should not be mapped to | |
111 | <tt class="exception">URLError</tt>). | |
112 | ||
113 | <P> | |
114 | This method will be called before any protocol-specific open method. | |
115 | </dl> | |
116 | ||
117 | <P> | |
118 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
119 | <td><nobr><b><var>protocol</var>_open</b>(</nobr></td> | |
120 | <td><var>req</var>)</td></tr></table></dt> | |
121 | <dd> | |
122 | This method is <em>not</em> defined in <tt class="class">BaseHandler</tt>, but | |
123 | subclasses should define it if they want to handle URLs with the given | |
124 | protocol. | |
125 | ||
126 | <P> | |
127 | This method, if defined, will be called by the parent | |
128 | <tt class="class">OpenerDirector</tt>. Return values should be the same as for | |
129 | <tt class="method">default_open()</tt>. | |
130 | </dl> | |
131 | ||
132 | <P> | |
133 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
134 | <td><nobr><b><tt id='l2h-3268' xml:id='l2h-3268' class="method">unknown_open</tt></b>(</nobr></td> | |
135 | <td><var>req</var>)</td></tr></table></dt> | |
136 | <dd> | |
137 | This method is <var>not</var> defined in <tt class="class">BaseHandler</tt>, but | |
138 | subclasses should define it if they want to catch all URLs with no | |
139 | specific registered handler to open it. | |
140 | ||
141 | <P> | |
142 | This method, if implemented, will be called by the <tt class="member">parent</tt> | |
143 | <tt class="class">OpenerDirector</tt>. Return values should be the same as for | |
144 | <tt class="method">default_open()</tt>. | |
145 | </dl> | |
146 | ||
147 | <P> | |
148 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
149 | <td><nobr><b><tt id='l2h-3269' xml:id='l2h-3269' class="method">http_error_default</tt></b>(</nobr></td> | |
150 | <td><var>req, fp, code, msg, hdrs</var>)</td></tr></table></dt> | |
151 | <dd> | |
152 | This method is <em>not</em> defined in <tt class="class">BaseHandler</tt>, but | |
153 | subclasses should override it if they intend to provide a catch-all | |
154 | for otherwise unhandled HTTP errors. It will be called automatically | |
155 | by the <tt class="class">OpenerDirector</tt> getting the error, and should not | |
156 | normally be called in other circumstances. | |
157 | ||
158 | <P> | |
159 | <var>req</var> will be a <tt class="class">Request</tt> object, <var>fp</var> will be a | |
160 | file-like object with the HTTP error body, <var>code</var> will be the | |
161 | three-digit code of the error, <var>msg</var> will be the user-visible | |
162 | explanation of the code and <var>hdrs</var> will be a mapping object with | |
163 | the headers of the error. | |
164 | ||
165 | <P> | |
166 | Return values and exceptions raised should be the same as those | |
167 | of <tt class="function">urlopen()</tt>. | |
168 | </dl> | |
169 | ||
170 | <P> | |
171 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
172 | <td><nobr><b><tt id='l2h-3270' xml:id='l2h-3270' class="method">http_error_<var>nnn</var></tt></b>(</nobr></td> | |
173 | <td><var>req, fp, code, msg, hdrs</var>)</td></tr></table></dt> | |
174 | <dd> | |
175 | <var>nnn</var> should be a three-digit HTTP error code. This method is | |
176 | also not defined in <tt class="class">BaseHandler</tt>, but will be called, if it | |
177 | exists, on an instance of a subclass, when an HTTP error with code | |
178 | <var>nnn</var> occurs. | |
179 | ||
180 | <P> | |
181 | Subclasses should override this method to handle specific HTTP | |
182 | errors. | |
183 | ||
184 | <P> | |
185 | Arguments, return values and exceptions raised should be the same as | |
186 | for <tt class="method">http_error_default()</tt>. | |
187 | </dl> | |
188 | ||
189 | <P> | |
190 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
191 | <td><nobr><b><var>protocol</var>_request</b>(</nobr></td> | |
192 | <td><var>req</var>)</td></tr></table></dt> | |
193 | <dd> | |
194 | This method is <em>not</em> defined in <tt class="class">BaseHandler</tt>, but | |
195 | subclasses should define it if they want to pre-process requests of | |
196 | the given protocol. | |
197 | ||
198 | <P> | |
199 | This method, if defined, will be called by the parent | |
200 | <tt class="class">OpenerDirector</tt>. <var>req</var> will be a <tt class="class">Request</tt> object. | |
201 | The return value should be a <tt class="class">Request</tt> object. | |
202 | </dl> | |
203 | ||
204 | <P> | |
205 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> | |
206 | <td><nobr><b><var>protocol</var>_response</b>(</nobr></td> | |
207 | <td><var>req, response</var>)</td></tr></table></dt> | |
208 | <dd> | |
209 | This method is <em>not</em> defined in <tt class="class">BaseHandler</tt>, but | |
210 | subclasses should define it if they want to post-process responses of | |
211 | the given protocol. | |
212 | ||
213 | <P> | |
214 | This method, if defined, will be called by the parent | |
215 | <tt class="class">OpenerDirector</tt>. <var>req</var> will be a <tt class="class">Request</tt> object. | |
216 | <var>response</var> will be an object implementing the same interface as | |
217 | the return value of <tt class="function">urlopen()</tt>. The return value should | |
218 | implement the same interface as the return value of | |
219 | <tt class="function">urlopen()</tt>. | |
220 | </dl> | |
221 | ||
222 | <P> | |
223 | ||
224 | <DIV CLASS="navigation"> | |
225 | <div class='online-navigation'> | |
226 | <p></p><hr /> | |
227 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> | |
228 | <tr> | |
229 | <td class='online-navigation'><a rel="prev" title="11.5.2 OpenerDirector Objects" | |
230 | href="opener-director-objects.html"><img src='../icons/previous.png' | |
231 | border='0' height='32' alt='Previous Page' width='32' /></A></td> | |
232 | <td class='online-navigation'><a rel="parent" title="11.5 urllib2 " | |
233 | href="module-urllib2.html"><img src='../icons/up.png' | |
234 | border='0' height='32' alt='Up One Level' width='32' /></A></td> | |
235 | <td class='online-navigation'><a rel="next" title="11.5.4 HTTPRedirectHandler Objects" | |
236 | href="http-redirect-handler.html"><img src='../icons/next.png' | |
237 | border='0' height='32' alt='Next Page' width='32' /></A></td> | |
238 | <td align="center" width="100%">Python Library Reference</td> | |
239 | <td class='online-navigation'><a rel="contents" title="Table of Contents" | |
240 | href="contents.html"><img src='../icons/contents.png' | |
241 | border='0' height='32' alt='Contents' width='32' /></A></td> | |
242 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' | |
243 | border='0' height='32' alt='Module Index' width='32' /></a></td> | |
244 | <td class='online-navigation'><a rel="index" title="Index" | |
245 | href="genindex.html"><img src='../icons/index.png' | |
246 | border='0' height='32' alt='Index' width='32' /></A></td> | |
247 | </tr></table> | |
248 | <div class='online-navigation'> | |
249 | <b class="navlabel">Previous:</b> | |
250 | <a class="sectref" rel="prev" href="opener-director-objects.html">11.5.2 OpenerDirector Objects</A> | |
251 | <b class="navlabel">Up:</b> | |
252 | <a class="sectref" rel="parent" href="module-urllib2.html">11.5 urllib2 </A> | |
253 | <b class="navlabel">Next:</b> | |
254 | <a class="sectref" rel="next" href="http-redirect-handler.html">11.5.4 HTTPRedirectHandler Objects</A> | |
255 | </div> | |
256 | </div> | |
257 | <hr /> | |
258 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> | |
259 | </DIV> | |
260 | <!--End of Navigation Panel--> | |
261 | <ADDRESS> | |
262 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. | |
263 | </ADDRESS> | |
264 | </BODY> | |
265 | </HTML> |