~dkuhlman/python-training-materials/Materials

« back to all changes in this revision

Viewing changes to python-2.7.10-docs-html/library/simplehttpserver.html

  • Committer: Dave Kuhlman
  • Date: 2016-02-11 21:17:09 UTC
  • Revision ID: dkuhlman@davekuhlman.org-20160211211709-03yaen3cjempbi2m
Updated Python 2.7 docs; added Python 3.5 docs

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2
 
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
 
 
4
 
 
5
 
<html xmlns="http://www.w3.org/1999/xhtml">
6
 
  <head>
7
 
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8
 
    
9
 
    <title>20.19. SimpleHTTPServer — Simple HTTP request handler &mdash; Python 2.7.10 documentation</title>
10
 
    
11
 
    <link rel="stylesheet" href="../_static/default.css" type="text/css" />
12
 
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
13
 
    
14
 
    <script type="text/javascript">
15
 
      var DOCUMENTATION_OPTIONS = {
16
 
        URL_ROOT:    '../',
17
 
        VERSION:     '2.7.10',
18
 
        COLLAPSE_INDEX: false,
19
 
        FILE_SUFFIX: '.html',
20
 
        HAS_SOURCE:  true
21
 
      };
22
 
    </script>
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>
39
 
 
40
 
    
41
 
 
42
 
  </head>
43
 
  <body>  
44
 
    <div class="related">
45
 
      <h3>Navigation</h3>
46
 
      <ul>
47
 
        <li class="right" style="margin-right: 10px">
48
 
          <a href="../genindex.html" title="General Index"
49
 
             accesskey="I">index</a></li>
50
 
        <li class="right" >
51
 
          <a href="../py-modindex.html" title="Python Module Index"
52
 
             >modules</a> |</li>
53
 
        <li class="right" >
54
 
          <a href="cgihttpserver.html" title="20.20. CGIHTTPServer — CGI-capable HTTP request handler"
55
 
             accesskey="N">next</a> |</li>
56
 
        <li class="right" >
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> &raquo;</li>
62
 
        <li>
63
 
          <span class="version_switcher_placeholder">2.7.10</span>
64
 
          <a href="../index.html">Documentation</a> &raquo;
65
 
        </li>
66
 
 
67
 
          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
68
 
          <li><a href="internet.html" accesskey="U">20. Internet Protocols and Support</a> &raquo;</li> 
69
 
      </ul>
70
 
    </div>    
71
 
 
72
 
    <div class="document">
73
 
      <div class="documentwrapper">
74
 
        <div class="bodywrapper">
75
 
          <div class="body">
76
 
            
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> &#8212; 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>
84
 
</div>
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>
89
 
<dl class="class">
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>
102
 
<dd></dd></dl>
103
 
 
104
 
<p>This will be <tt class="docutils literal"><span class="pre">&quot;SimpleHTTP/&quot;</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>
113
 
</dd></dl>
114
 
 
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>
116
 
<dl class="method">
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>
122
 
</dd></dl>
123
 
 
124
 
<dl class="method">
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&#8217;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&#8217;s size and a
142
 
<tt class="docutils literal"><span class="pre">'Last-Modified:'</span></tt> header with the file&#8217;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&#8217;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>
148
 
as the Handler.</p>
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>
151
 
</div>
152
 
</dd></dl>
153
 
 
154
 
</dd></dl>
155
 
 
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
158
 
directory.</p>
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>
161
 
 
162
 
<span class="n">PORT</span> <span class="o">=</span> <span class="mi">8000</span>
163
 
 
164
 
<span class="n">Handler</span> <span class="o">=</span> <span class="n">SimpleHTTPServer</span><span class="o">.</span><span class="n">SimpleHTTPRequestHandler</span>
165
 
 
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">&quot;&quot;</span><span class="p">,</span> <span class="n">PORT</span><span class="p">),</span> <span class="n">Handler</span><span class="p">)</span>
167
 
 
168
 
<span class="k">print</span> <span class="s">&quot;serving at port&quot;</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>
170
 
</pre></div>
171
 
</div>
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
177
 
</pre></div>
178
 
</div>
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>
184
 
</dl>
185
 
</div>
186
 
</div>
187
 
 
188
 
 
189
 
          </div>
190
 
        </div>
191
 
      </div>
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> &#8212; Basic HTTP server</a></p>
197
 
  <h4>Next topic</h4>
198
 
  <p class="topless"><a href="cgihttpserver.html"
199
 
                        title="next chapter">20.20. <tt class="docutils literal"><span class="pre">CGIHTTPServer</span></tt> &#8212; CGI-capable HTTP request handler</a></p>
200
 
<h3>This Page</h3>
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>
205
 
</ul>
206
 
 
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" />
214
 
    </form>
215
 
    <p class="searchtip" style="font-size: 90%">
216
 
    Enter search terms or a module, class or function name.
217
 
    </p>
218
 
</div>
219
 
<script type="text/javascript">$('#searchbox').show(0);</script>
220
 
        </div>
221
 
      </div>
222
 
      <div class="clearer"></div>
223
 
    </div>  
224
 
    <div class="related">
225
 
      <h3>Navigation</h3>
226
 
      <ul>
227
 
        <li class="right" style="margin-right: 10px">
228
 
          <a href="../genindex.html" title="General Index"
229
 
             >index</a></li>
230
 
        <li class="right" >
231
 
          <a href="../py-modindex.html" title="Python Module Index"
232
 
             >modules</a> |</li>
233
 
        <li class="right" >
234
 
          <a href="cgihttpserver.html" title="20.20. CGIHTTPServer — CGI-capable HTTP request handler"
235
 
             >next</a> |</li>
236
 
        <li class="right" >
237
 
          <a href="basehttpserver.html" title="20.18. BaseHTTPServer — Basic HTTP server"
238
 
             >previous</a> |</li>
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> &raquo;</li>
242
 
        <li>
243
 
          <span class="version_switcher_placeholder">2.7.10</span>
244
 
          <a href="../index.html">Documentation</a> &raquo;
245
 
        </li>
246
 
 
247
 
          <li><a href="index.html" >The Python Standard Library</a> &raquo;</li>
248
 
          <li><a href="internet.html" >20. Internet Protocols and Support</a> &raquo;</li> 
249
 
      </ul>
250
 
    </div>  
251
 
    <div class="footer">
252
 
    &copy; <a href="../copyright.html">Copyright</a> 1990-2015, Python Software Foundation.
253
 
    <br />
254
 
    The Python Software Foundation is a non-profit corporation.
255
 
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
256
 
    <br />
257
 
    Last updated on May 23, 2015.
258
 
    <a href="../bugs.html">Found a bug</a>?
259
 
    <br />
260
 
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.2.3.
261
 
    </div>
262
 
 
263
 
  </body>
264
 
</html>
 
 
b'\\ No newline at end of file'