<!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=
"module-SimpleHTTPServer.html" />
<link rel=
"prev" href=
"module-SocketServer.html" />
<link rel=
"parent" href=
"internet.html" />
<link rel=
"next" href=
"module-SimpleHTTPServer.html" />
<meta name='aesop' content='information'
/>
<title>11.17 BaseHTTPServer -- Basic HTTP server
</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.16.3 RequestHandler Objects"
href=
"node537.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. Internet Protocols and"
href=
"internet.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.18 SimpleHTTPServer "
href=
"module-SimpleHTTPServer.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=
"node537.html">11.16.3 RequestHandler Objects
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"internet.html">11. Internet Protocols and
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"module-SimpleHTTPServer.html">11.18 SimpleHTTPServer
</A>
<!--End of Navigation Panel-->
<H1><A NAME=
"SECTION00131700000000000000000">
11.17 <tt class=
"module">BaseHTTPServer
</tt> --
<A NAME=
"module-BaseHTTPServer"></A>
<a id='l2h-
3569' xml:id='l2h-
3569'
></a><a id='l2h-
3570' xml:id='l2h-
3570'
></a><a id='l2h-
3601' xml:id='l2h-
3601'
></a>
This module defines two classes for implementing HTTP servers
(Web servers). Usually, this module isn't used directly, but is used
as a basis for building functioning Web servers. See the
<tt class=
"module"><a href=
"module-SimpleHTTPServer.html">SimpleHTTPServer
</a></tt><a id='l2h-
3602' xml:id='l2h-
3602'
></a> and
<tt class=
"module"><a href=
"module-CGIHTTPServer.html">CGIHTTPServer
</a></tt><a id='l2h-
3603' xml:id='l2h-
3603'
></a> modules.
The first class,
<tt class=
"class">HTTPServer
</tt>, is a
<tt class=
"class">SocketServer.TCPServer
</tt> subclass. It creates and listens at the
HTTP socket, dispatching the requests to a handler. Code to create and
run the server looks like this:
<div class=
"verbatim"><pre>
def run(server_class=BaseHTTPServer.HTTPServer,
handler_class=BaseHTTPServer.BaseHTTPRequestHandler):
server_address = ('',
8000)
httpd = server_class(server_address, handler_class)
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><span class=
"typelabel">class
</span> <tt id='l2h-
3571' xml:id='l2h-
3571'
class=
"class">HTTPServer
</tt></b>(
</nobr></td>
<td><var>server_address, RequestHandlerClass
</var>)
</td></tr></table></dt>
This class builds on the
<tt class=
"class">TCPServer
</tt> class by
storing the server address as instance
variables named
<tt class=
"member">server_name
</tt> and
<tt class=
"member">server_port
</tt>. The
server is accessible by the handler, typically through the handler's
<tt class=
"member">server
</tt> instance variable.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><span class=
"typelabel">class
</span> <tt id='l2h-
3572' xml:id='l2h-
3572'
class=
"class">BaseHTTPRequestHandler
</tt></b>(
</nobr></td>
<td><var>request, client_address, server
</var>)
</td></tr></table></dt>
to handle the HTTP requests that arrive at the server. By itself,
it cannot respond to any actual HTTP requests; it must be subclassed
to handle each request method (e.g. GET or POST).
<tt class=
"class">BaseHTTPRequestHandler
</tt> provides a number of class and instance
variables, and methods for use by subclasses.
The handler will parse the request and the headers, then call a
method specific to the request type. The method name is constructed
from the request. For example, for the request method
"<tt class="samp
">SPAM</tt>", the
<tt class=
"method">do_SPAM()
</tt> method will be called with no arguments. All of
the relevant information is stored in instance variables of the
handler. Subclasses should not need to override or extend the
<tt class=
"method">__init__()
</tt> method.
<tt class=
"class">BaseHTTPRequestHandler
</tt> has the following instance variables:
<dl><dt><b><tt id='l2h-
3573' xml:id='l2h-
3573'
class=
"member">client_address
</tt></b></dt>
Contains a tuple of the form
<code>(
<var>host
</var>,
<var>port
</var>)
</code> referring
<dl><dt><b><tt id='l2h-
3574' xml:id='l2h-
3574'
class=
"member">command
</tt></b></dt>
Contains the command (request type). For example,
<code>'GET'
</code>.
<dl><dt><b><tt id='l2h-
3575' xml:id='l2h-
3575'
class=
"member">path
</tt></b></dt>
Contains the request path.
<dl><dt><b><tt id='l2h-
3576' xml:id='l2h-
3576'
class=
"member">request_version
</tt></b></dt>
Contains the version string from the request. For example,
<dl><dt><b><tt id='l2h-
3577' xml:id='l2h-
3577'
class=
"member">headers
</tt></b></dt>
Holds an instance of the class specified by the
<tt class=
"member">MessageClass
</tt>
class variable. This instance parses and manages the headers in
<dl><dt><b><tt id='l2h-
3578' xml:id='l2h-
3578'
class=
"member">rfile
</tt></b></dt>
Contains an input stream, positioned at the start of the optional
<dl><dt><b><tt id='l2h-
3579' xml:id='l2h-
3579'
class=
"member">wfile
</tt></b></dt>
Contains the output stream for writing a response back to the client.
Proper adherence to the HTTP protocol must be used when writing
<tt class=
"class">BaseHTTPRequestHandler
</tt> has the following class variables:
<dl><dt><b><tt id='l2h-
3580' xml:id='l2h-
3580'
class=
"member">server_version
</tt></b></dt>
Specifies the server software version. You may want to override
The format is multiple whitespace-separated strings,
where each string is of the form name[/version].
For example,
<code>'BaseHTTP/
0.2'
</code>.
<dl><dt><b><tt id='l2h-
3581' xml:id='l2h-
3581'
class=
"member">sys_version
</tt></b></dt>
Contains the Python system version, in a form usable by the
<tt class=
"member">version_string
</tt> method and the
<tt class=
"member">server_version
</tt> class
variable. For example,
<code>'Python/
1.4'
</code>.
<dl><dt><b><tt id='l2h-
3582' xml:id='l2h-
3582'
class=
"member">error_message_format
</tt></b></dt>
Specifies a format string for building an error response to the
client. It uses parenthesized, keyed format specifiers, so the
format operand must be a dictionary. The
<var>code
</var> key should
be an integer, specifying the numeric HTTP error code value.
<var>message
</var> should be a string containing a (detailed) error
message of what occurred, and
<var>explain
</var> should be an
explanation of the error code number. Default
<var>message
</var>
and
<var>explain
</var> values can found in the
<var>responses
</var>
<dl><dt><b><tt id='l2h-
3583' xml:id='l2h-
3583'
class=
"member">protocol_version
</tt></b></dt>
This specifies the HTTP protocol version used in responses. If set
to
<code>'HTTP/
1.1'
</code>, the server will permit HTTP persistent
connections; however, your server
<em>must
</em> then include an
accurate
<code>Content-Length
</code> header (using
<tt class=
"method">send_header()
</tt>)
in all of its responses to clients. For backwards compatibility,
the setting defaults to
<code>'HTTP/
1.0'
</code>.
<dl><dt><b><tt id='l2h-
3584' xml:id='l2h-
3584'
class=
"member">MessageClass
</tt></b></dt>
Specifies a
<tt class=
"class">rfc822.Message
</tt>-like class to parse HTTP
headers. Typically, this is not overridden, and it defaults to
<tt class=
"class">mimetools.Message
</tt>.
<a id='l2h-
3586' xml:id='l2h-
3586'
></a></dl>
<dl><dt><b><tt id='l2h-
3587' xml:id='l2h-
3587'
class=
"member">responses
</tt></b></dt>
This variable contains a mapping of error code integers to two-element
tuples containing a short and long message. For example,
<code>{
<var>code
</var>: (
<var>shortmessage
</var>,
<var>longmessage
</var>)}
</code>. The
<var>shortmessage
</var> is usually used as the
<var>message
</var> key in an
error response, and
<var>longmessage
</var> as the
<var>explain
</var> key
(see the
<tt class=
"member">error_message_format
</tt> class variable).
A
<tt class=
"class">BaseHTTPRequestHandler
</tt> instance has the following methods:
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3588' xml:id='l2h-
3588'
class=
"method">handle
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Calls
<tt class=
"method">handle_one_request()
</tt> once (or, if persistent connections
are enabled, multiple times) to handle incoming HTTP requests.
You should never need to override it; instead, implement appropriate
<tt class=
"method">do_*()
</tt> methods.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3589' xml:id='l2h-
3589'
class=
"method">handle_one_request
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
This method will parse and dispatch
the request to the appropriate
<tt class=
"method">do_*()
</tt> method. You should
never need to override it.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3590' xml:id='l2h-
3590'
class=
"method">send_error
</tt></b>(
</nobr></td>
<td><var>code
</var><big>[
</big><var>, message
</var><big>]
</big><var></var>)
</td></tr></table></dt>
Sends and logs a complete error reply to the client. The numeric
<var>code
</var> specifies the HTTP error code, with
<var>message
</var> as
optional, more specific text. A complete set of headers is sent,
followed by text composed using the
<tt class=
"member">error_message_format
</tt>
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3591' xml:id='l2h-
3591'
class=
"method">send_response
</tt></b>(
</nobr></td>
<td><var>code
</var><big>[
</big><var>, message
</var><big>]
</big><var></var>)
</td></tr></table></dt>
Sends a response header and logs the accepted request. The HTTP
response line is sent, followed by
<em>Server
</em> and
<em>Date
</em>
headers. The values for these two headers are picked up from the
<tt class=
"method">version_string()
</tt> and
<tt class=
"method">date_time_string()
</tt> methods,
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3592' xml:id='l2h-
3592'
class=
"method">send_header
</tt></b>(
</nobr></td>
<td><var>keyword, value
</var>)
</td></tr></table></dt>
Writes a specific HTTP header to the output stream.
<var>keyword
</var>
should specify the header keyword, with
<var>value
</var> specifying
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3593' xml:id='l2h-
3593'
class=
"method">end_headers
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Sends a blank line, indicating the end of the HTTP headers in
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3594' xml:id='l2h-
3594'
class=
"method">log_request
</tt></b>(
</nobr></td>
<td><var></var><big>[
</big><var>code
</var><big>[
</big><var>, size
</var><big>]
</big><var></var><big>]
</big><var></var>)
</td></tr></table></dt>
Logs an accepted (successful) request.
<var>code
</var> should specify
the numeric HTTP code associated with the response. If a size of
the response is available, then it should be passed as the
<var>size
</var> parameter.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3595' xml:id='l2h-
3595'
class=
"method">log_error
</tt></b>(
</nobr></td>
<td><var>...
</var>)
</td></tr></table></dt>
Logs an error when a request cannot be fulfilled. By default,
it passes the message to
<tt class=
"method">log_message()
</tt>, so it takes the
same arguments (
<var>format
</var> and additional values).
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3596' xml:id='l2h-
3596'
class=
"method">log_message
</tt></b>(
</nobr></td>
<td><var>format, ...
</var>)
</td></tr></table></dt>
Logs an arbitrary message to
<code>sys.stderr
</code>. This is typically
overridden to create custom error logging mechanisms. The
<var>format
</var> argument is a standard printf-style format string,
where the additional arguments to
<tt class=
"method">log_message()
</tt> are applied
as inputs to the formatting. The client address and current date
and time are prefixed to every message logged.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3597' xml:id='l2h-
3597'
class=
"method">version_string
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Returns the server software's version string. This is a combination
of the
<tt class=
"member">server_version
</tt> and
<tt class=
"member">sys_version
</tt> class variables.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3598' xml:id='l2h-
3598'
class=
"method">date_time_string
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Returns the current date and time, formatted for a message header.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3599' xml:id='l2h-
3599'
class=
"method">log_data_time_string
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Returns the current date and time, formatted for logging.
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3600' xml:id='l2h-
3600'
class=
"method">address_string
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
Returns the client address, formatted for logging. A name lookup
is performed on the client's IP address.
<p class=
"heading">See Also:
</p>
<dl compact=
"compact" class=
"seemodule">
<dt>Module
<b><tt class=
"module"><a href=
"module-CGIHTTPServer.html">CGIHTTPServer
</a></tt>:
</b>
<dd>Extended request handler that supports CGI
<dl compact=
"compact" class=
"seemodule">
<dt>Module
<b><tt class=
"module"><a href=
"module-SimpleHTTPServer.html">SimpleHTTPServer
</a></tt>:
</b>
<dd>Basic request handler that limits response
to files actually under the document root.
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"11.16.3 RequestHandler Objects"
href=
"node537.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. Internet Protocols and"
href=
"internet.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.18 SimpleHTTPServer "
href=
"module-SimpleHTTPServer.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=
"node537.html">11.16.3 RequestHandler Objects
</A>
<b class=
"navlabel">Up:
</b>
<a class=
"sectref" rel=
"parent" href=
"internet.html">11. Internet Protocols and
</A>
<b class=
"navlabel">Next:
</b>
<a class=
"sectref" rel=
"next" href=
"module-SimpleHTTPServer.html">11.18 SimpleHTTPServer
</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.