<!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-CGIHTTPServer.html" />
<link rel=
"prev" href=
"module-BaseHTTPServer.html" />
<link rel=
"parent" href=
"internet.html" />
<link rel=
"next" href=
"module-CGIHTTPServer.html" />
<meta name='aesop' content='information'
/>
<title>11.18 SimpleHTTPServer -- Simple HTTP request handler
</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.17 BaseHTTPServer "
href=
"module-BaseHTTPServer.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.19 CGIHTTPServer "
href=
"module-CGIHTTPServer.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=
"module-BaseHTTPServer.html">11.17 BaseHTTPServer
</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-CGIHTTPServer.html">11.19 CGIHTTPServer
</A>
<!--End of Navigation Panel-->
<H1><A NAME=
"SECTION00131800000000000000000">
11.18 <tt class=
"module">SimpleHTTPServer
</tt> --
Simple HTTP request handler
</A>
<A NAME=
"module-SimpleHTTPServer"></A>
The
<tt class=
"module">SimpleHTTPServer
</tt> module defines a request-handler class,
interface-compatible with
<tt class=
"class">BaseHTTPServer.BaseHTTPRequestHandler
</tt>,
that serves files only from a base directory.
The
<tt class=
"module">SimpleHTTPServer
</tt> module defines the following class:
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><span class=
"typelabel">class
</span> <tt id='l2h-
3605' xml:id='l2h-
3605'
class=
"class">SimpleHTTPRequestHandler
</tt></b>(
</nobr></td>
<td><var>request, client_address, server
</var>)
</td></tr></table></dt>
This class is used to serve files from the current directory and below,
directly mapping the directory structure to HTTP requests.
A lot of the work, such as parsing the request, is done by the base
class
<tt class=
"class">BaseHTTPServer.BaseHTTPRequestHandler
</tt>. This class
implements the
<tt class=
"function">do_GET()
</tt> and
<tt class=
"function">do_HEAD()
</tt> functions.
The
<tt class=
"class">SimpleHTTPRequestHandler
</tt> defines the following member
<dl><dt><b><tt id='l2h-
3606' xml:id='l2h-
3606'
class=
"member">server_version
</tt></b></dt>
This will be
<code>"SimpleHTTP/" + __version__
</code>, where
<code>__version__
</code>
is defined in the module.
<dl><dt><b><tt id='l2h-
3607' xml:id='l2h-
3607'
class=
"member">extensions_map
</tt></b></dt>
A dictionary mapping suffixes into MIME types. The default is signified
by an empty string, and is considered to be
<code>application/octet-stream
</code>.
The mapping is used case-insensitively, and so should contain only
The
<tt class=
"class">SimpleHTTPRequestHandler
</tt> defines the following methods:
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3608' xml:id='l2h-
3608'
class=
"method">do_HEAD
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
This method serves the
<code>'HEAD'
</code> request type: it sends the
headers it would send for the equivalent
<code>GET
</code> request. See the
<tt class=
"method">do_GET()
</tt> method for a more complete explanation of the possible
<dl><dt><table cellpadding=
"0" cellspacing=
"0"><tr valign=
"baseline">
<td><nobr><b><tt id='l2h-
3609' xml:id='l2h-
3609'
class=
"method">do_GET
</tt></b>(
</nobr></td>
<td><var></var>)
</td></tr></table></dt>
The request is mapped to a local file by interpreting the request as
a path relative to the current working directory.
If the request was mapped to a directory, the directory is checked for
a file named
<code>index.html
</code> or
<code>index.htm
</code> (in that order).
If found, the file's contents are returned; otherwise a directory
listing is generated by calling the
<tt class=
"method">list_directory()
</tt> method.
This method uses
<tt class=
"function">os.listdir()
</tt> to scan the directory, and
returns a
<code>404</code> error response if the
<tt class=
"function">listdir()
</tt> fails.
If the request was mapped to a file, it is opened and the contents are
returned. Any
<tt class=
"exception">IOError
</tt> exception in opening the requested
file is mapped to a
<code>404</code>,
<code>'File not found'
</code>
error. Otherwise, the content type is guessed by calling the
<tt class=
"method">guess_type()
</tt> method, which in turn uses the
<var>extensions_map
</var> variable.
A
<code>'Content-type:'
</code> header with the guessed content type is
output, followed by a blank line signifying the end of the headers,
and then the contents of the file are output. If the file's MIME type
starts with
<code>text/
</code> the file is opened in text mode; otherwise
For example usage, see the implementation of the
<tt class=
"function">test()
</tt>
<p class=
"heading">See Also:
</p>
<dl compact=
"compact" class=
"seemodule">
<dt>Module
<b><tt class=
"module"><a href=
"module-BaseHTTPServer.html">BaseHTTPServer
</a></tt>:
</b>
<dd>Base class implementation for Web server
<div class='online-navigation'
>
<table align=
"center" width=
"100%" cellpadding=
"0" cellspacing=
"2">
<td class='online-navigation'
><a rel=
"prev" title=
"11.17 BaseHTTPServer "
href=
"module-BaseHTTPServer.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.19 CGIHTTPServer "
href=
"module-CGIHTTPServer.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=
"module-BaseHTTPServer.html">11.17 BaseHTTPServer
</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-CGIHTTPServer.html">11.19 CGIHTTPServer
</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.