Sophie

Sophie

distrib > Mageia > 7 > armv7hl > media > core-updates > by-pkgid > e1011ddec34cda34f3a002b121247943 > files > 914

python-docs-2.7.17-1.1.mga7.noarch.rpm


<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta charset="utf-8" />
    <title>20.19. SimpleHTTPServer — Simple HTTP request handler &#8212; Python 2.7.17 documentation</title>
    <link rel="stylesheet" href="../_static/classic.css" type="text/css" />
    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
    
    <script type="text/javascript" id="documentation_options" data-url_root="../" src="../_static/documentation_options.js"></script>
    <script type="text/javascript" src="../_static/jquery.js"></script>
    <script type="text/javascript" src="../_static/underscore.js"></script>
    <script type="text/javascript" src="../_static/doctools.js"></script>
    <script type="text/javascript" src="../_static/language_data.js"></script>
    
    <script type="text/javascript" src="../_static/sidebar.js"></script>
    
    <link rel="search" type="application/opensearchdescription+xml"
          title="Search within Python 2.7.17 documentation"
          href="../_static/opensearch.xml"/>
    <link rel="author" title="About these documents" href="../about.html" />
    <link rel="index" title="Index" href="../genindex.html" />
    <link rel="search" title="Search" href="../search.html" />
    <link rel="copyright" title="Copyright" href="../copyright.html" />
    <link rel="next" title="20.20. CGIHTTPServer — CGI-capable HTTP request handler" href="cgihttpserver.html" />
    <link rel="prev" title="20.18. BaseHTTPServer — Basic HTTP server" href="basehttpserver.html" />
    <link rel="shortcut icon" type="image/png" href="../_static/py.png" />
    <link rel="canonical" href="https://docs.python.org/2/library/simplehttpserver.html" />
    <script type="text/javascript" src="../_static/copybutton.js"></script>
    
 
    

  </head><body>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="cgihttpserver.html" title="20.20. CGIHTTPServer — CGI-capable HTTP request handler"
             accesskey="N">next</a> |</li>
        <li class="right" >
          <a href="basehttpserver.html" title="20.18. BaseHTTPServer — Basic HTTP server"
             accesskey="P">previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">Python 2.7.17 documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">20. Internet Protocols and Support</a> &#187;</li> 
      </ul>
    </div>    

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body" role="main">
            
  <div class="section" id="module-SimpleHTTPServer">
<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."><code class="xref py py-mod docutils literal notranslate"><span class="pre">SimpleHTTPServer</span></code></a> — Simple HTTP request handler<a class="headerlink" href="#module-SimpleHTTPServer" title="Permalink to this headline">¶</a></h1>
<div class="admonition note">
<p class="admonition-title">Note</p>
<p>The <a class="reference internal" href="#module-SimpleHTTPServer" title="SimpleHTTPServer: This module provides a basic request handler for HTTP servers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">SimpleHTTPServer</span></code></a> module has been merged into <code class="xref py py-mod docutils literal notranslate"><span class="pre">http.server</span></code> in
Python 3.  The <a class="reference internal" href="../glossary.html#term-2to3"><span class="xref std std-term">2to3</span></a> tool will automatically adapt imports when
converting your sources to Python 3.</p>
</div>
<div class="admonition warning">
<p class="admonition-title">Warning</p>
<p><a class="reference internal" href="#module-SimpleHTTPServer" title="SimpleHTTPServer: This module provides a basic request handler for HTTP servers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">SimpleHTTPServer</span></code></a> is not recommended for production. It only implements
basic security checks.</p>
</div>
<p>The <a class="reference internal" href="#module-SimpleHTTPServer" title="SimpleHTTPServer: This module provides a basic request handler for HTTP servers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">SimpleHTTPServer</span></code></a> module defines a single class,
<a class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler" title="SimpleHTTPServer.SimpleHTTPRequestHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleHTTPRequestHandler</span></code></a>, which is interface-compatible with
<a class="reference internal" href="basehttpserver.html#BaseHTTPServer.BaseHTTPRequestHandler" title="BaseHTTPServer.BaseHTTPRequestHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHTTPServer.BaseHTTPRequestHandler</span></code></a>.</p>
<p>The <a class="reference internal" href="#module-SimpleHTTPServer" title="SimpleHTTPServer: This module provides a basic request handler for HTTP servers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">SimpleHTTPServer</span></code></a> module defines the following class:</p>
<dl class="class">
<dt id="SimpleHTTPServer.SimpleHTTPRequestHandler">
<em class="property">class </em><code class="descclassname">SimpleHTTPServer.</code><code class="descname">SimpleHTTPRequestHandler</code><span class="sig-paren">(</span><em>request</em>, <em>client_address</em>, <em>server</em><span class="sig-paren">)</span><a class="headerlink" href="#SimpleHTTPServer.SimpleHTTPRequestHandler" title="Permalink to this definition">¶</a></dt>
<dd><p>This class serves files from the current directory and below, directly
mapping the directory structure to HTTP requests.</p>
<p>A lot of the work, such as parsing the request, is done by the base class
<a class="reference internal" href="basehttpserver.html#BaseHTTPServer.BaseHTTPRequestHandler" title="BaseHTTPServer.BaseHTTPRequestHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHTTPServer.BaseHTTPRequestHandler</span></code></a>.  This class implements the
<a class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET" title="SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET"><code class="xref py py-func docutils literal notranslate"><span class="pre">do_GET()</span></code></a> and <a class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.do_HEAD" title="SimpleHTTPServer.SimpleHTTPRequestHandler.do_HEAD"><code class="xref py py-func docutils literal notranslate"><span class="pre">do_HEAD()</span></code></a> functions.</p>
<p>The following are defined as class-level attributes of
<a class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler" title="SimpleHTTPServer.SimpleHTTPRequestHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleHTTPRequestHandler</span></code></a>:</p>
<dl class="attribute">
<dt id="SimpleHTTPServer.SimpleHTTPRequestHandler.server_version">
<code class="descname">server_version</code><a class="headerlink" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.server_version" title="Permalink to this definition">¶</a></dt>
<dd></dd></dl>

<p>This will be <code class="docutils literal notranslate"><span class="pre">&quot;SimpleHTTP/&quot;</span> <span class="pre">+</span> <span class="pre">__version__</span></code>, where <code class="docutils literal notranslate"><span class="pre">__version__</span></code> is
defined at the module level.</p>
<dl class="attribute">
<dt id="SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map">
<code class="descname">extensions_map</code><a class="headerlink" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map" title="Permalink to this definition">¶</a></dt>
<dd><p>A dictionary mapping suffixes into MIME types. The default is
signified by an empty string, and is considered to be
<code class="docutils literal notranslate"><span class="pre">application/octet-stream</span></code>. The mapping is used case-insensitively,
and so should contain only lower-cased keys.</p>
</dd></dl>

<p>The <a class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler" title="SimpleHTTPServer.SimpleHTTPRequestHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleHTTPRequestHandler</span></code></a> class defines the following methods:</p>
<dl class="method">
<dt id="SimpleHTTPServer.SimpleHTTPRequestHandler.do_HEAD">
<code class="descname">do_HEAD</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.do_HEAD" title="Permalink to this definition">¶</a></dt>
<dd><p>This method serves the <code class="docutils literal notranslate"><span class="pre">'HEAD'</span></code> request type: it sends the headers it
would send for the equivalent <code class="docutils literal notranslate"><span class="pre">GET</span></code> request. See the <a class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET" title="SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET"><code class="xref py py-meth docutils literal notranslate"><span class="pre">do_GET()</span></code></a>
method for a more complete explanation of the possible headers.</p>
</dd></dl>

<dl class="method">
<dt id="SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET">
<code class="descname">do_GET</code><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET" title="Permalink to this definition">¶</a></dt>
<dd><p>The request is mapped to a local file by interpreting the request as a
path relative to the current working directory.</p>
<p>If the request was mapped to a directory, the directory is checked for a
file named <code class="docutils literal notranslate"><span class="pre">index.html</span></code> or <code class="docutils literal notranslate"><span class="pre">index.htm</span></code> (in that order). If found, the
file’s contents are returned; otherwise a directory listing is generated
by calling the <code class="xref py py-meth docutils literal notranslate"><span class="pre">list_directory()</span></code> method. This method uses
<a class="reference internal" href="os.html#os.listdir" title="os.listdir"><code class="xref py py-func docutils literal notranslate"><span class="pre">os.listdir()</span></code></a> to scan the directory, and returns a <code class="docutils literal notranslate"><span class="pre">404</span></code> error
response if the <code class="xref py py-func docutils literal notranslate"><span class="pre">listdir()</span></code> fails.</p>
<p>If the request was mapped to a file, it is opened and the contents are
returned.  Any <a class="reference internal" href="exceptions.html#exceptions.IOError" title="exceptions.IOError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">IOError</span></code></a> exception in opening the requested file is
mapped to a <code class="docutils literal notranslate"><span class="pre">404</span></code>, <code class="docutils literal notranslate"><span class="pre">'File</span> <span class="pre">not</span> <span class="pre">found'</span></code> error. Otherwise, the content
type is guessed by calling the <code class="xref py py-meth docutils literal notranslate"><span class="pre">guess_type()</span></code> method, which in turn
uses the <em>extensions_map</em> variable.</p>
<p>A <code class="docutils literal notranslate"><span class="pre">'Content-type:'</span></code> header with the guessed content type is output,
followed by a <code class="docutils literal notranslate"><span class="pre">'Content-Length:'</span></code> header with the file’s size and a
<code class="docutils literal notranslate"><span class="pre">'Last-Modified:'</span></code> header with the file’s modification time.</p>
<p>Then follows 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 class="docutils literal notranslate"><span class="pre">text/</span></code> the file is opened in text mode; otherwise binary mode is used.</p>
<p>The <a class="reference internal" href="test.html#module-test" title="test: Regression tests package containing the testing suite for Python."><code class="xref py py-func docutils literal notranslate"><span class="pre">test()</span></code></a> function in the <a class="reference internal" href="#module-SimpleHTTPServer" title="SimpleHTTPServer: This module provides a basic request handler for HTTP servers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">SimpleHTTPServer</span></code></a> module is an
example which creates a server using the <a class="reference internal" href="#SimpleHTTPServer.SimpleHTTPRequestHandler" title="SimpleHTTPServer.SimpleHTTPRequestHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleHTTPRequestHandler</span></code></a>
as the Handler.</p>
<div class="versionadded">
<p><span class="versionmodified added">New in version 2.5: </span>The <code class="docutils literal notranslate"><span class="pre">'Last-Modified'</span></code> header.</p>
</div>
</dd></dl>

</dd></dl>

<p>The <a class="reference internal" href="#module-SimpleHTTPServer" title="SimpleHTTPServer: This module provides a basic request handler for HTTP servers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">SimpleHTTPServer</span></code></a> module can be used in the following manner in order
to set up a very basic web server serving files relative to the current
directory.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span> <span class="nn">SimpleHTTPServer</span>
<span class="kn">import</span> <span class="nn">SocketServer</span>

<span class="n">PORT</span> <span class="o">=</span> <span class="mi">8000</span>

<span class="n">Handler</span> <span class="o">=</span> <span class="n">SimpleHTTPServer</span><span class="o">.</span><span class="n">SimpleHTTPRequestHandler</span>

<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="s2">&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>

<span class="nb">print</span> <span class="s2">&quot;serving at port&quot;</span><span class="p">,</span> <span class="n">PORT</span>
<span class="n">httpd</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>
</pre></div>
</div>
<p>The <a class="reference internal" href="#module-SimpleHTTPServer" title="SimpleHTTPServer: This module provides a basic request handler for HTTP servers."><code class="xref py py-mod docutils literal notranslate"><span class="pre">SimpleHTTPServer</span></code></a> module can also be invoked directly using the
<a class="reference internal" href="../using/cmdline.html#cmdoption-m"><code class="xref std std-option docutils literal notranslate"><span class="pre">-m</span></code></a> switch of the interpreter with a <code class="docutils literal notranslate"><span class="pre">port</span> <span class="pre">number</span></code> argument.
Similar to the previous example, this serves the files relative to the
current directory.</p>
<div class="highlight-default notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">SimpleHTTPServer</span> <span class="mi">8000</span>
</pre></div>
</div>
<div class="admonition seealso">
<p class="admonition-title">See also</p>
<dl class="simple">
<dt>Module <a class="reference internal" href="basehttpserver.html#module-BaseHTTPServer" title="BaseHTTPServer: Basic HTTP server (base class for SimpleHTTPServer and CGIHTTPServer)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">BaseHTTPServer</span></code></a></dt><dd><p>Base class implementation for Web server and request handler.</p>
</dd>
</dl>
</div>
</div>


          </div>
        </div>
      </div>
      <div class="sphinxsidebar" role="navigation" aria-label="main navigation">
        <div class="sphinxsidebarwrapper">
  <h4>Previous topic</h4>
  <p class="topless"><a href="basehttpserver.html"
                        title="previous chapter">20.18. <code class="xref py py-mod docutils literal notranslate"><span class="pre">BaseHTTPServer</span></code> — Basic HTTP server</a></p>
  <h4>Next topic</h4>
  <p class="topless"><a href="cgihttpserver.html"
                        title="next chapter">20.20. <code class="xref py py-mod docutils literal notranslate"><span class="pre">CGIHTTPServer</span></code> — CGI-capable HTTP request handler</a></p>
  <div role="note" aria-label="source link">
    <h3>This Page</h3>
    <ul class="this-page-menu">
      <li><a href="../_sources/library/simplehttpserver.rst.txt"
            rel="nofollow">Show Source</a></li>
    </ul>
   </div>
<div id="searchbox" style="display: none" role="search">
  <h3>Quick search</h3>
    <div class="searchformwrapper">
    <form class="search" action="../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
    </form>
    </div>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>  
    <div class="related" role="navigation" aria-label="related navigation">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../py-modindex.html" title="Python Module Index"
             >modules</a> |</li>
        <li class="right" >
          <a href="cgihttpserver.html" title="20.20. CGIHTTPServer — CGI-capable HTTP request handler"
             >next</a> |</li>
        <li class="right" >
          <a href="basehttpserver.html" title="20.18. BaseHTTPServer — Basic HTTP server"
             >previous</a> |</li>
        <li><img src="../_static/py.png" alt=""
                 style="vertical-align: middle; margin-top: -1px"/></li>
        <li><a href="https://www.python.org/">Python</a> &#187;</li>
        <li>
          <a href="../index.html">Python 2.7.17 documentation</a> &#187;
        </li>

          <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> &#187;</li>
          <li class="nav-item nav-item-2"><a href="internet.html" >20. Internet Protocols and Support</a> &#187;</li> 
      </ul>
    </div>  
    <div class="footer">
    &copy; <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation.
    <br />
    The Python Software Foundation is a non-profit corporation.
    <a href="https://www.python.org/psf/donations/">Please donate.</a>
    <br />
    Last updated on Oct 19, 2019.
    <a href="../bugs.html">Found a bug</a>?
    <br />
    Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 2.0.1.
    </div>

  </body>
</html>