<!DOCTYPE html PUBLIC
"-//W3C//DTD HTML 4.0 Transitional//EN">
<link rel=
"STYLESHEET" href=
"lib.css" type='text/css'
/>
<link rel=
"SHORTCUT ICON" href=
"../icons/pyfav.png" type=
"image/png" />
<link rel='start' href='../index.html' title='Python Documentation Index'
/>
<link rel=
"first" href=
"lib.html" title='Python Library Reference'
/>
<link rel='contents' href='contents.html'
title=
"Contents" />
<link rel='index' href='genindex.html' title='Index'
/>
<link rel='last' href='about.html' title='About this document...'
/>
<link rel='help' href='about.html' title='About this document...'
/>
<link rel=
"next" href=
"http-redirect-handler.html" />
<link rel=
"prev" href=
"opener-director-objects.html" />
<link rel=
"parent" href=
"module-urllib2.html" />
<link rel=
"next" href=
"http-redirect-handler.html" />
<meta name='aesop' content='information'
/>
<title>11.5.3 BaseHandler Objects
</title>
<div id='top-navigation-panel' xml:id='top-navigation-panel'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"11.5.2 OpenerDirector Objects"
href=
"opener-director-objects.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"11.5 urllib2 "
href=
"module-urllib2.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"11.5.4 HTTPRedirectHandler Objects"
href=
"http-redirect-handler.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"opener-director-objects.html">11.5.2 OpenerDirector Objects
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-urllib2.html">11.5 urllib2
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"http-redirect-handler.html">11.5.4 HTTPRedirectHandler Objects
</A>
<!--End of Navigation Panel-->
<H2><A NAME=
"SECTION0013530000000000000000"></A><A NAME=
"base-handler-objects"></A>
11.5.3 BaseHandler Objects
<tt class=
"class">BaseHandler
</tt> objects provide a couple of methods that are
directly useful, and others that are meant to be used by derived
classes. These are intended for direct use:
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3264' xml:id='l2h-
3264'
class=
"method">add_parent
</tt></b>(
</nobr></td>
<td><var>director
</var>)
</td></tr></table></dt>
Add a director as parent.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3265' xml:id='l2h-
3265'
class=
"method">close
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
The following members and methods should only be used by classes
derived from
<tt class=
"class">BaseHandler
</tt>.
<span class=
"note"><b class=
"label">Note:
</b>
adopted that subclasses defining
<tt class=
"method"><var>protocol
</var>_request()
</tt> or
<tt class=
"method"><var>protocol
</var>_response()
</tt> methods are named
<tt class=
"class">*Processor
</tt>; all others are named
<tt class=
"class">*Handler
</tt>.
</span>
<dl><dt><b><tt id='l2h-
3266' xml:id='l2h-
3266'
class=
"member">parent
</tt></b></dt>
A valid
<tt class=
"class">OpenerDirector
</tt>, which can be used to open using a
different protocol, or handle errors.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3267' xml:id='l2h-
3267'
class=
"method">default_open
</tt></b>(
</nobr></td>
<td><var>req
</var>)
</td></tr></table></dt>
This method is
<em>not
</em> defined in
<tt class=
"class">BaseHandler
</tt>, but
subclasses should define it if they want to catch all URLs.
This method, if implemented, will be called by the parent
<tt class=
"class">OpenerDirector
</tt>. It should return a file-like object as
described in the return value of the
<tt class=
"method">open()
</tt> of
<tt class=
"class">OpenerDirector
</tt>, or
<code>None
</code>. It should raise
<tt class=
"exception">URLError
</tt>, unless a truly exceptional thing happens (for
example,
<tt class=
"exception">MemoryError
</tt> should not be mapped to
<tt class=
"exception">URLError
</tt>).
This method will be called before any protocol-specific open method.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><var>protocol
</var>_open
</b>(
</nobr></td>
<td><var>req
</var>)
</td></tr></table></dt>
This method is
<em>not
</em> defined in
<tt class=
"class">BaseHandler
</tt>, but
subclasses should define it if they want to handle URLs with the given
This method, if defined, will be called by the parent
<tt class=
"class">OpenerDirector
</tt>. Return values should be the same as for
<tt class=
"method">default_open()
</tt>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3268' xml:id='l2h-
3268'
class=
"method">unknown_open
</tt></b>(
</nobr></td>
<td><var>req
</var>)
</td></tr></table></dt>
This method is
<var>not
</var> defined in
<tt class=
"class">BaseHandler
</tt>, but
subclasses should define it if they want to catch all URLs with no
specific registered handler to open it.
This method, if implemented, will be called by the
<tt class=
"member">parent
</tt>
<tt class=
"class">OpenerDirector
</tt>. Return values should be the same as for
<tt class=
"method">default_open()
</tt>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3269' xml:id='l2h-
3269'
class=
"method">http_error_default
</tt></b>(
</nobr></td>
<td><var>req, fp, code, msg, hdrs
</var>)
</td></tr></table></dt>
This method is
<em>not
</em> defined in
<tt class=
"class">BaseHandler
</tt>, but
subclasses should override it if they intend to provide a catch-all
for otherwise unhandled HTTP errors. It will be called automatically
by the
<tt class=
"class">OpenerDirector
</tt> getting the error, and should not
normally be called in other circumstances.
<var>req
</var> will be a
<tt class=
"class">Request
</tt> object,
<var>fp
</var> will be a
file-like object with the HTTP error body,
<var>code
</var> will be the
three-digit code of the error,
<var>msg
</var> will be the user-visible
explanation of the code and
<var>hdrs
</var> will be a mapping object with
the headers of the error.
Return values and exceptions raised should be the same as those
of
<tt class=
"function">urlopen()
</tt>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3270' xml:id='l2h-
3270'
class=
"method">http_error_
<var>nnn
</var></tt></b>(
</nobr></td>
<td><var>req, fp, code, msg, hdrs
</var>)
</td></tr></table></dt>
<var>nnn
</var> should be a three-digit HTTP error code. This method is
also not defined in
<tt class=
"class">BaseHandler
</tt>, but will be called, if it
exists, on an instance of a subclass, when an HTTP error with code
Subclasses should override this method to handle specific HTTP
Arguments, return values and exceptions raised should be the same as
for
<tt class=
"method">http_error_default()
</tt>.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><var>protocol
</var>_request
</b>(
</nobr></td>
<td><var>req
</var>)
</td></tr></table></dt>
This method is
<em>not
</em> defined in
<tt class=
"class">BaseHandler
</tt>, but
subclasses should define it if they want to pre-process requests of
This method, if defined, will be called by the parent
<tt class=
"class">OpenerDirector
</tt>.
<var>req
</var> will be a
<tt class=
"class">Request
</tt> object.
The return value should be a
<tt class=
"class">Request
</tt> object.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><var>protocol
</var>_response
</b>(
</nobr></td>
<td><var>req, response
</var>)
</td></tr></table></dt>
This method is
<em>not
</em> defined in
<tt class=
"class">BaseHandler
</tt>, but
subclasses should define it if they want to post-process responses of
This method, if defined, will be called by the parent
<tt class=
"class">OpenerDirector
</tt>.
<var>req
</var> will be a
<tt class=
"class">Request
</tt> object.
<var>response
</var> will be an object implementing the same interface as
the return value of
<tt class=
"function">urlopen()
</tt>. The return value should
implement the same interface as the return value of
<tt class=
"function">urlopen()
</tt>.
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"11.5.2 OpenerDirector Objects"
href=
"opener-director-objects.html"><img src='../icons/previous.png'
border='
0' height='
32' alt='Previous Page' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"parent" title=
"11.5 urllib2 "
href=
"module-urllib2.html"><img src='../icons/up.png'
border='
0' height='
32' alt='Up One Level' width='
32'
/></A></td>
<td class='online-navigation'
><a rel=
"next" title=
"11.5.4 HTTPRedirectHandler Objects"
href=
"http-redirect-handler.html"><img src='../icons/next.png'
border='
0' height='
32' alt='Next Page' width='
32'
/></A></td>
<td align=
"center" width=
"100%">Python Library Reference
</td>
<td class='online-navigation'
><a rel=
"contents" title=
"Table of Contents"
href=
"contents.html"><img src='../icons/contents.png'
border='
0' height='
32' alt='Contents' width='
32'
/></A></td>
<td class='online-navigation'
><a href=
"modindex.html" title=
"Module Index"><img src='../icons/modules.png'
border='
0' height='
32' alt='Module Index' width='
32'
/></a></td>
<td class='online-navigation'
><a rel=
"index" title=
"Index"
href=
"genindex.html"><img src='../icons/index.png'
border='
0' height='
32' alt='Index' width='
32'
/></A></td>
<div class='online-navigation'
>
<b class=
"navlabel">Previous:
</b>
<a class=
"sectref" rel=
"prev" href=
"opener-director-objects.html">11.5.2 OpenerDirector Objects
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"module-urllib2.html">11.5 urllib2
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"http-redirect-handler.html">11.5.4 HTTPRedirectHandler Objects
</A>
<span class=
"release-info">Release
2.4.2, documentation updated on
28 September
2005.
</span>
<!--End of Navigation Panel-->
See
<i><a href=
"about.html">About this document...
</a></i> for information on suggesting changes.