1
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
5
<html xmlns="http://www.w3.org/1999/xhtml">
7
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
9
<title>20.19. SimpleHTTPServer — Simple HTTP request handler — Python 2.7.10 documentation</title>
11
<link rel="stylesheet" href="../_static/default.css" type="text/css" />
12
<link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
14
<script type="text/javascript">
15
var DOCUMENTATION_OPTIONS = {
18
COLLAPSE_INDEX: false,
23
<script type="text/javascript" src="../_static/jquery.js"></script>
24
<script type="text/javascript" src="../_static/underscore.js"></script>
25
<script type="text/javascript" src="../_static/doctools.js"></script>
26
<script type="text/javascript" src="../_static/sidebar.js"></script>
27
<link rel="search" type="application/opensearchdescription+xml"
28
title="Search within Python 2.7.10 documentation"
29
href="../_static/opensearch.xml"/>
30
<link rel="author" title="About these documents" href="../about.html" />
31
<link rel="copyright" title="Copyright" href="../copyright.html" />
32
<link rel="top" title="Python 2.7.10 documentation" href="../index.html" />
33
<link rel="up" title="20. Internet Protocols and Support" href="internet.html" />
34
<link rel="next" title="20.20. CGIHTTPServer — CGI-capable HTTP request handler" href="cgihttpserver.html" />
35
<link rel="prev" title="20.18. BaseHTTPServer — Basic HTTP server" href="basehttpserver.html" />
36
<link rel="shortcut icon" type="image/png" href="../_static/py.png" />
37
<script type="text/javascript" src="../_static/copybutton.js"></script>
38
<script type="text/javascript" src="../_static/version_switch.js"></script>
47
<li class="right" style="margin-right: 10px">
48
<a href="../genindex.html" title="General Index"
49
accesskey="I">index</a></li>
51
<a href="../py-modindex.html" title="Python Module Index"
54
<a href="cgihttpserver.html" title="20.20. CGIHTTPServer — CGI-capable HTTP request handler"
55
accesskey="N">next</a> |</li>
57
<a href="basehttpserver.html" title="20.18. BaseHTTPServer — Basic HTTP server"
58
accesskey="P">previous</a> |</li>
59
<li><img src="../_static/py.png" alt=""
60
style="vertical-align: middle; margin-top: -1px"/></li>
61
<li><a href="https://www.python.org/">Python</a> »</li>
63
<span class="version_switcher_placeholder">2.7.10</span>
64
<a href="../index.html">Documentation</a> »
67
<li><a href="index.html" >The Python Standard Library</a> »</li>
68
<li><a href="internet.html" accesskey="U">20. Internet Protocols and Support</a> »</li>
72
<div class="document">
73
<div class="documentwrapper">
74
<div class="bodywrapper">
77
<div class="section" id="module-SimpleHTTPServer">
78
<span id="simplehttpserver-simple-http-request-handler"></span><h1>20.19. <a class="reference internal" href="#module-SimpleHTTPServer" title="SimpleHTTPServer: This module provides a basic request handler for HTTP servers."><tt class="xref py py-mod docutils literal"><span class="pre">SimpleHTTPServer</span></tt></a> — Simple HTTP request handler<a class="headerlink" href="#module-SimpleHTTPServer" title="Permalink to this headline">¶</a></h1>
79
<div class="admonition note">
80
<p class="first admonition-title">Note</p>
81
<p class="last">The <a class="reference internal" href="#module-SimpleHTTPServer" title="SimpleHTTPServer: This module provides a basic request handler for HTTP servers."><tt class="xref py py-mod docutils literal"><span class="pre">SimpleHTTPServer</span></tt></a> module has been merged into <tt class="xref py py-mod docutils literal"><span class="pre">http.server</span></tt> in
82
Python 3. The <a class="reference internal" href="../glossary.html#term-to3"><em class="xref std std-term">2to3</em></a> tool will automatically adapt imports when
83
converting your sources to Python 3.</p>
85
<p>The <a class="reference internal" href="#module-SimpleHTTPServer" title="SimpleHTTPServer: This module provides a basic request handler for HTTP servers."><tt class="xref py py-mod docutils literal"><span class="pre">SimpleHTTPServer</span></tt></a> module defines a single class,
86
<a class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler" title="SimpleHTTPServer.SimpleHTTPRequestHandler"><tt class="xref py py-class docutils literal"><span class="pre">SimpleHTTPRequestHandler</span></tt></a>, which is interface-compatible with
87
<a class="reference internal" href="basehttpserver.html#BaseHTTPServer.BaseHTTPRequestHandler" title="BaseHTTPServer.BaseHTTPRequestHandler"><tt class="xref py py-class docutils literal"><span class="pre">BaseHTTPServer.BaseHTTPRequestHandler</span></tt></a>.</p>
88
<p>The <a class="reference internal" href="#module-SimpleHTTPServer" title="SimpleHTTPServer: This module provides a basic request handler for HTTP servers."><tt class="xref py py-mod docutils literal"><span class="pre">SimpleHTTPServer</span></tt></a> module defines the following class:</p>
90
<dt id="SimpleHTTPServer.SimpleHTTPRequestHandler">
91
<em class="property">class </em><tt class="descclassname">SimpleHTTPServer.</tt><tt class="descname">SimpleHTTPRequestHandler</tt><big>(</big><em>request</em>, <em>client_address</em>, <em>server</em><big>)</big><a class="headerlink" href="#SimpleHTTPServer.SimpleHTTPRequestHandler" title="Permalink to this definition">¶</a></dt>
92
<dd><p>This class serves files from the current directory and below, directly
93
mapping the directory structure to HTTP requests.</p>
94
<p>A lot of the work, such as parsing the request, is done by the base class
95
<a class="reference internal" href="basehttpserver.html#BaseHTTPServer.BaseHTTPRequestHandler" title="BaseHTTPServer.BaseHTTPRequestHandler"><tt class="xref py py-class docutils literal"><span class="pre">BaseHTTPServer.BaseHTTPRequestHandler</span></tt></a>. This class implements the
96
<a class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET" title="SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET"><tt class="xref py py-func docutils literal"><span class="pre">do_GET()</span></tt></a> and <a class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.do_HEAD" title="SimpleHTTPServer.SimpleHTTPRequestHandler.do_HEAD"><tt class="xref py py-func docutils literal"><span class="pre">do_HEAD()</span></tt></a> functions.</p>
97
<p>The following are defined as class-level attributes of
98
<a class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler" title="SimpleHTTPServer.SimpleHTTPRequestHandler"><tt class="xref py py-class docutils literal"><span class="pre">SimpleHTTPRequestHandler</span></tt></a>:</p>
99
<dl class="attribute">
100
<dt id="SimpleHTTPServer.SimpleHTTPRequestHandler.server_version">
101
<tt class="descname">server_version</tt><a class="headerlink" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.server_version" title="Permalink to this definition">¶</a></dt>
104
<p>This will be <tt class="docutils literal"><span class="pre">"SimpleHTTP/"</span> <span class="pre">+</span> <span class="pre">__version__</span></tt>, where <tt class="docutils literal"><span class="pre">__version__</span></tt> is
105
defined at the module level.</p>
106
<dl class="attribute">
107
<dt id="SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map">
108
<tt class="descname">extensions_map</tt><a class="headerlink" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map" title="Permalink to this definition">¶</a></dt>
109
<dd><p>A dictionary mapping suffixes into MIME types. The default is
110
signified by an empty string, and is considered to be
111
<tt class="docutils literal"><span class="pre">application/octet-stream</span></tt>. The mapping is used case-insensitively,
112
and so should contain only lower-cased keys.</p>
115
<p>The <a class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler" title="SimpleHTTPServer.SimpleHTTPRequestHandler"><tt class="xref py py-class docutils literal"><span class="pre">SimpleHTTPRequestHandler</span></tt></a> class defines the following methods:</p>
117
<dt id="SimpleHTTPServer.SimpleHTTPRequestHandler.do_HEAD">
118
<tt class="descname">do_HEAD</tt><big>(</big><big>)</big><a class="headerlink" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.do_HEAD" title="Permalink to this definition">¶</a></dt>
119
<dd><p>This method serves the <tt class="docutils literal"><span class="pre">'HEAD'</span></tt> request type: it sends the headers it
120
would send for the equivalent <tt class="docutils literal"><span class="pre">GET</span></tt> request. See the <a class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET" title="SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET"><tt class="xref py py-meth docutils literal"><span class="pre">do_GET()</span></tt></a>
121
method for a more complete explanation of the possible headers.</p>
125
<dt id="SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET">
126
<tt class="descname">do_GET</tt><big>(</big><big>)</big><a class="headerlink" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET" title="Permalink to this definition">¶</a></dt>
127
<dd><p>The request is mapped to a local file by interpreting the request as a
128
path relative to the current working directory.</p>
129
<p>If the request was mapped to a directory, the directory is checked for a
130
file named <tt class="docutils literal"><span class="pre">index.html</span></tt> or <tt class="docutils literal"><span class="pre">index.htm</span></tt> (in that order). If found, the
131
file’s contents are returned; otherwise a directory listing is generated
132
by calling the <tt class="xref py py-meth docutils literal"><span class="pre">list_directory()</span></tt> method. This method uses
133
<a class="reference internal" href="os.html#os.listdir" title="os.listdir"><tt class="xref py py-func docutils literal"><span class="pre">os.listdir()</span></tt></a> to scan the directory, and returns a <tt class="docutils literal"><span class="pre">404</span></tt> error
134
response if the <tt class="xref py py-func docutils literal"><span class="pre">listdir()</span></tt> fails.</p>
135
<p>If the request was mapped to a file, it is opened and the contents are
136
returned. Any <a class="reference internal" href="exceptions.html#exceptions.IOError" title="exceptions.IOError"><tt class="xref py py-exc docutils literal"><span class="pre">IOError</span></tt></a> exception in opening the requested file is
137
mapped to a <tt class="docutils literal"><span class="pre">404</span></tt>, <tt class="docutils literal"><span class="pre">'File</span> <span class="pre">not</span> <span class="pre">found'</span></tt> error. Otherwise, the content
138
type is guessed by calling the <tt class="xref py py-meth docutils literal"><span class="pre">guess_type()</span></tt> method, which in turn
139
uses the <em>extensions_map</em> variable.</p>
140
<p>A <tt class="docutils literal"><span class="pre">'Content-type:'</span></tt> header with the guessed content type is output,
141
followed by a <tt class="docutils literal"><span class="pre">'Content-Length:'</span></tt> header with the file’s size and a
142
<tt class="docutils literal"><span class="pre">'Last-Modified:'</span></tt> header with the file’s modification time.</p>
143
<p>Then follows a blank line signifying the end of the headers, and then the
144
contents of the file are output. If the file’s MIME type starts with
145
<tt class="docutils literal"><span class="pre">text/</span></tt> the file is opened in text mode; otherwise binary mode is used.</p>
146
<p>The <a class="reference internal" href="test.html#module-test" title="test: Regression tests package containing the testing suite for Python."><tt class="xref py py-func docutils literal"><span class="pre">test()</span></tt></a> function in the <a class="reference internal" href="#module-SimpleHTTPServer" title="SimpleHTTPServer: This module provides a basic request handler for HTTP servers."><tt class="xref py py-mod docutils literal"><span class="pre">SimpleHTTPServer</span></tt></a> module is an
147
example which creates a server using the <a class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler" title="SimpleHTTPServer.SimpleHTTPRequestHandler"><tt class="xref py py-class docutils literal"><span class="pre">SimpleHTTPRequestHandler</span></tt></a>
149
<div class="versionadded">
150
<p><span class="versionmodified">New in version 2.5: </span>The <tt class="docutils literal"><span class="pre">'Last-Modified'</span></tt> header.</p>
156
<p>The <a class="reference internal" href="#module-SimpleHTTPServer" title="SimpleHTTPServer: This module provides a basic request handler for HTTP servers."><tt class="xref py py-mod docutils literal"><span class="pre">SimpleHTTPServer</span></tt></a> module can be used in the following manner in order
157
to set up a very basic web server serving files relative to the current
159
<div class="highlight-python"><div class="highlight"><pre><span class="kn">import</span> <span class="nn">SimpleHTTPServer</span>
160
<span class="kn">import</span> <span class="nn">SocketServer</span>
162
<span class="n">PORT</span> <span class="o">=</span> <span class="mi">8000</span>
164
<span class="n">Handler</span> <span class="o">=</span> <span class="n">SimpleHTTPServer</span><span class="o">.</span><span class="n">SimpleHTTPRequestHandler</span>
166
<span class="n">httpd</span> <span class="o">=</span> <span class="n">SocketServer</span><span class="o">.</span><span class="n">TCPServer</span><span class="p">((</span><span class="s">""</span><span class="p">,</span> <span class="n">PORT</span><span class="p">),</span> <span class="n">Handler</span><span class="p">)</span>
168
<span class="k">print</span> <span class="s">"serving at port"</span><span class="p">,</span> <span class="n">PORT</span>
169
<span class="n">httpd</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
172
<p>The <a class="reference internal" href="#module-SimpleHTTPServer" title="SimpleHTTPServer: This module provides a basic request handler for HTTP servers."><tt class="xref py py-mod docutils literal"><span class="pre">SimpleHTTPServer</span></tt></a> module can also be invoked directly using the
173
<a class="reference internal" href="../using/cmdline.html#cmdoption-m"><em class="xref std std-option">-m</em></a> switch of the interpreter with a <tt class="docutils literal"><span class="pre">port</span> <span class="pre">number</span></tt> argument.
174
Similar to the previous example, this serves the files relative to the
175
current directory.</p>
176
<div class="highlight-python"><div class="highlight"><pre>python -m SimpleHTTPServer 8000
179
<div class="admonition seealso">
180
<p class="first admonition-title">See also</p>
181
<dl class="last docutils">
182
<dt>Module <a class="reference internal" href="basehttpserver.html#module-BaseHTTPServer" title="BaseHTTPServer: Basic HTTP server (base class for SimpleHTTPServer and CGIHTTPServer)."><tt class="xref py py-mod docutils literal"><span class="pre">BaseHTTPServer</span></tt></a></dt>
183
<dd>Base class implementation for Web server and request handler.</dd>
192
<div class="sphinxsidebar">
193
<div class="sphinxsidebarwrapper">
194
<h4>Previous topic</h4>
195
<p class="topless"><a href="basehttpserver.html"
196
title="previous chapter">20.18. <tt class="docutils literal"><span class="pre">BaseHTTPServer</span></tt> — Basic HTTP server</a></p>
198
<p class="topless"><a href="cgihttpserver.html"
199
title="next chapter">20.20. <tt class="docutils literal"><span class="pre">CGIHTTPServer</span></tt> — CGI-capable HTTP request handler</a></p>
201
<ul class="this-page-menu">
202
<li><a href="../bugs.html">Report a Bug</a></li>
203
<li><a href="../_sources/library/simplehttpserver.txt"
204
rel="nofollow">Show Source</a></li>
207
<div id="searchbox" style="display: none">
208
<h3>Quick search</h3>
209
<form class="search" action="../search.html" method="get">
210
<input type="text" name="q" />
211
<input type="submit" value="Go" />
212
<input type="hidden" name="check_keywords" value="yes" />
213
<input type="hidden" name="area" value="default" />
215
<p class="searchtip" style="font-size: 90%">
216
Enter search terms or a module, class or function name.
219
<script type="text/javascript">$('#searchbox').show(0);</script>
222
<div class="clearer"></div>
224
<div class="related">
227
<li class="right" style="margin-right: 10px">
228
<a href="../genindex.html" title="General Index"
231
<a href="../py-modindex.html" title="Python Module Index"
234
<a href="cgihttpserver.html" title="20.20. CGIHTTPServer — CGI-capable HTTP request handler"
237
<a href="basehttpserver.html" title="20.18. BaseHTTPServer — Basic HTTP server"
239
<li><img src="../_static/py.png" alt=""
240
style="vertical-align: middle; margin-top: -1px"/></li>
241
<li><a href="https://www.python.org/">Python</a> »</li>
243
<span class="version_switcher_placeholder">2.7.10</span>
244
<a href="../index.html">Documentation</a> »
247
<li><a href="index.html" >The Python Standard Library</a> »</li>
248
<li><a href="internet.html" >20. Internet Protocols and Support</a> »</li>
252
© <a href="../copyright.html">Copyright</a> 1990-2015, Python Software Foundation.
254
The Python Software Foundation is a non-profit corporation.
255
<a href="https://www.python.org/psf/donations/">Please donate.</a>
257
Last updated on May 23, 2015.
258
<a href="../bugs.html">Found a bug</a>?
260
Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2.3.
b'\\ No newline at end of file'