| 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="base-handler-objects.html" /> |
| 13 | <link rel="prev" href="request-objects.html" /> |
| 14 | <link rel="parent" href="module-urllib2.html" /> |
| 15 | <link rel="next" href="base-handler-objects.html" /> |
| 16 | <meta name='aesop' content='information' /> |
| 17 | <title>11.5.2 OpenerDirector 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.1 Request Objects" |
| 25 | href="request-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.3 BaseHandler Objects" |
| 31 | href="base-handler-objects.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="request-objects.html">11.5.1 Request 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="base-handler-objects.html">11.5.3 BaseHandler Objects</A> |
| 50 | </div> |
| 51 | <hr /></div> |
| 52 | </DIV> |
| 53 | <!--End of Navigation Panel--> |
| 54 | |
| 55 | <H2><A NAME="SECTION0013520000000000000000"></A><A NAME="opener-director-objects"></A> |
| 56 | <BR> |
| 57 | 11.5.2 OpenerDirector Objects |
| 58 | </H2> |
| 59 | |
| 60 | <P> |
| 61 | <tt class="class">OpenerDirector</tt> instances have the following methods: |
| 62 | |
| 63 | <P> |
| 64 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 65 | <td><nobr><b><tt id='l2h-3261' xml:id='l2h-3261' class="method">add_handler</tt></b>(</nobr></td> |
| 66 | <td><var>handler</var>)</td></tr></table></dt> |
| 67 | <dd> |
| 68 | <var>handler</var> should be an instance of <tt class="class">BaseHandler</tt>. The |
| 69 | following methods are searched, and added to the possible chains (note |
| 70 | that HTTP errors are a special case). |
| 71 | |
| 72 | <P> |
| 73 | |
| 74 | <UL> |
| 75 | <LI><tt class="method"><var>protocol</var>_open()</tt> -- |
| 76 | signal that the handler knows how to open <var>protocol</var> URLs. |
| 77 | </LI> |
| 78 | <LI><tt class="method">http_error_<var>type</var>()</tt> -- |
| 79 | signal that the handler knows how to handle HTTP errors with HTTP |
| 80 | error code <var>type</var>. |
| 81 | </LI> |
| 82 | <LI><tt class="method"><var>protocol</var>_error()</tt> -- |
| 83 | signal that the handler knows how to handle errors from |
| 84 | (non-<code>http</code>) <var>protocol</var>. |
| 85 | </LI> |
| 86 | <LI><tt class="method"><var>protocol</var>_request()</tt> -- |
| 87 | signal that the handler knows how to pre-process <var>protocol</var> |
| 88 | requests. |
| 89 | </LI> |
| 90 | <LI><tt class="method"><var>protocol</var>_response()</tt> -- |
| 91 | signal that the handler knows how to post-process <var>protocol</var> |
| 92 | responses. |
| 93 | </LI> |
| 94 | </UL> |
| 95 | </dl> |
| 96 | |
| 97 | <P> |
| 98 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 99 | <td><nobr><b><tt id='l2h-3262' xml:id='l2h-3262' class="method">open</tt></b>(</nobr></td> |
| 100 | <td><var>url</var><big>[</big><var>, data</var><big>]</big><var></var>)</td></tr></table></dt> |
| 101 | <dd> |
| 102 | Open the given <var>url</var> (which can be a request object or a string), |
| 103 | optionally passing the given <var>data</var>. |
| 104 | Arguments, return values and exceptions raised are the same as those |
| 105 | of <tt class="function">urlopen()</tt> (which simply calls the <tt class="method">open()</tt> method |
| 106 | on the currently installed global <tt class="class">OpenerDirector</tt>). |
| 107 | </dl> |
| 108 | |
| 109 | <P> |
| 110 | <dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline"> |
| 111 | <td><nobr><b><tt id='l2h-3263' xml:id='l2h-3263' class="method">error</tt></b>(</nobr></td> |
| 112 | <td><var>proto</var><big>[</big><var>, |
| 113 | arg</var><big>[</big><var>, ...</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt> |
| 114 | <dd> |
| 115 | Handle an error of the given protocol. This will call the registered |
| 116 | error handlers for the given protocol with the given arguments (which |
| 117 | are protocol specific). The HTTP protocol is a special case which |
| 118 | uses the HTTP response code to determine the specific error handler; |
| 119 | refer to the <tt class="method">http_error_*()</tt> methods of the handler classes. |
| 120 | |
| 121 | <P> |
| 122 | Return values and exceptions raised are the same as those |
| 123 | of <tt class="function">urlopen()</tt>. |
| 124 | </dl> |
| 125 | |
| 126 | <P> |
| 127 | OpenerDirector objects open URLs in three stages: |
| 128 | |
| 129 | <P> |
| 130 | The order in which these methods are called within each stage is |
| 131 | determined by sorting the handler instances. |
| 132 | |
| 133 | <P> |
| 134 | |
| 135 | <OL> |
| 136 | <LI>Every handler with a method named like |
| 137 | <tt class="method"><var>protocol</var>_request()</tt> has that method called to |
| 138 | pre-process the request. |
| 139 | |
| 140 | <P> |
| 141 | </LI> |
| 142 | <LI>Handlers with a method named like |
| 143 | <tt class="method"><var>protocol</var>_open()</tt> are called to handle the request. |
| 144 | This stage ends when a handler either returns a |
| 145 | non-<tt class="constant">None</tt> value (ie. a response), or raises an exception |
| 146 | (usually URLError). Exceptions are allowed to propagate. |
| 147 | |
| 148 | <P> |
| 149 | In fact, the above algorithm is first tried for methods named |
| 150 | <tt class="method">default_open</tt>. If all such methods return |
| 151 | <tt class="constant">None</tt>, the algorithm is repeated for methods named like |
| 152 | <tt class="method"><var>protocol</var>_open()</tt>. If all such methods return |
| 153 | <tt class="constant">None</tt>, the algorithm is repeated for methods named |
| 154 | <tt class="method">unknown_open()</tt>. |
| 155 | |
| 156 | <P> |
| 157 | Note that the implementation of these methods may involve calls of |
| 158 | the parent <tt class="class">OpenerDirector</tt> instance's <tt class="method">.open()</tt> and |
| 159 | <tt class="method">.error()</tt> methods. |
| 160 | |
| 161 | <P> |
| 162 | </LI> |
| 163 | <LI>Every handler with a method named like |
| 164 | <tt class="method"><var>protocol</var>_response()</tt> has that method called to |
| 165 | post-process the response. |
| 166 | |
| 167 | <P> |
| 168 | </LI> |
| 169 | </OL> |
| 170 | |
| 171 | <P> |
| 172 | |
| 173 | <DIV CLASS="navigation"> |
| 174 | <div class='online-navigation'> |
| 175 | <p></p><hr /> |
| 176 | <table align="center" width="100%" cellpadding="0" cellspacing="2"> |
| 177 | <tr> |
| 178 | <td class='online-navigation'><a rel="prev" title="11.5.1 Request Objects" |
| 179 | href="request-objects.html"><img src='../icons/previous.png' |
| 180 | border='0' height='32' alt='Previous Page' width='32' /></A></td> |
| 181 | <td class='online-navigation'><a rel="parent" title="11.5 urllib2 " |
| 182 | href="module-urllib2.html"><img src='../icons/up.png' |
| 183 | border='0' height='32' alt='Up One Level' width='32' /></A></td> |
| 184 | <td class='online-navigation'><a rel="next" title="11.5.3 BaseHandler Objects" |
| 185 | href="base-handler-objects.html"><img src='../icons/next.png' |
| 186 | border='0' height='32' alt='Next Page' width='32' /></A></td> |
| 187 | <td align="center" width="100%">Python Library Reference</td> |
| 188 | <td class='online-navigation'><a rel="contents" title="Table of Contents" |
| 189 | href="contents.html"><img src='../icons/contents.png' |
| 190 | border='0' height='32' alt='Contents' width='32' /></A></td> |
| 191 | <td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png' |
| 192 | border='0' height='32' alt='Module Index' width='32' /></a></td> |
| 193 | <td class='online-navigation'><a rel="index" title="Index" |
| 194 | href="genindex.html"><img src='../icons/index.png' |
| 195 | border='0' height='32' alt='Index' width='32' /></A></td> |
| 196 | </tr></table> |
| 197 | <div class='online-navigation'> |
| 198 | <b class="navlabel">Previous:</b> |
| 199 | <a class="sectref" rel="prev" href="request-objects.html">11.5.1 Request Objects</A> |
| 200 | <b class="navlabel">Up:</b> |
| 201 | <a class="sectref" rel="parent" href="module-urllib2.html">11.5 urllib2 </A> |
| 202 | <b class="navlabel">Next:</b> |
| 203 | <a class="sectref" rel="next" href="base-handler-objects.html">11.5.3 BaseHandler Objects</A> |
| 204 | </div> |
| 205 | </div> |
| 206 | <hr /> |
| 207 | <span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span> |
| 208 | </DIV> |
| 209 | <!--End of Navigation Panel--> |
| 210 | <ADDRESS> |
| 211 | See <i><a href="about.html">About this document...</a></i> for information on suggesting changes. |
| 212 | </ADDRESS> |
| 213 | </BODY> |
| 214 | </HTML> |