korsygfhrtzangaiide
Elepffwdsff
/
usr
/
share
/
doc
/
python-docs-2.7.5
/
html
/
library
/
Upload FileeE
HOME
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>20.19. SimpleHTTPServer — Simple HTTP request handler — Python 2.7.5 documentation</title> <link rel="stylesheet" href="../_static/default.css" type="text/css" /> <link rel="stylesheet" href="../_static/pygments.css" type="text/css" /> <script type="text/javascript"> var DOCUMENTATION_OPTIONS = { URL_ROOT: '../', VERSION: '2.7.5', COLLAPSE_INDEX: false, FILE_SUFFIX: '.html', HAS_SOURCE: true }; </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/sidebar.js"></script> <link rel="search" type="application/opensearchdescription+xml" title="Search within Python 2.7.5 documentation" href="../_static/opensearch.xml"/> <link rel="author" title="About these documents" href="../about.html" /> <link rel="copyright" title="Copyright" href="../copyright.html" /> <link rel="top" title="Python 2.7.5 documentation" href="../index.html" /> <link rel="up" title="20. Internet Protocols and Support" href="internet.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" /> <script type="text/javascript" src="../_static/copybutton.js"></script> </head> <body> <div class="related"> <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="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="internet.html" accesskey="U">20. Internet Protocols and Support</a> »</li> </ul> </div> <div class="document"> <div class="documentwrapper"> <div class="bodywrapper"> <div class="body"> <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."><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> <div class="admonition note"> <p class="first admonition-title">Note</p> <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 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 converting your sources to Python 3.</p> </div> <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, <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 <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> <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> <dl class="class"> <dt id="SimpleHTTPServer.SimpleHTTPRequestHandler"> <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> <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"><tt class="xref py py-class docutils literal"><span class="pre">BaseHTTPServer.BaseHTTPRequestHandler</span></tt></a>. This class implements the <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> <p>The following are defined as class-level attributes of <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> <dl class="attribute"> <dt id="SimpleHTTPServer.SimpleHTTPRequestHandler.server_version"> <tt class="descname">server_version</tt><a class="headerlink" href="#SimpleHTTPServer.SimpleHTTPRequestHandler.server_version" title="Permalink to this definition">¶</a></dt> <dd></dd></dl> <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 defined at the module level.</p> <dl class="attribute"> <dt id="SimpleHTTPServer.SimpleHTTPRequestHandler.extensions_map"> <tt class="descname">extensions_map</tt><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 <tt class="docutils literal"><span class="pre">application/octet-stream</span></tt>. 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"><tt class="xref py py-class docutils literal"><span class="pre">SimpleHTTPRequestHandler</span></tt></a> class defines the following methods:</p> <dl class="method"> <dt id="SimpleHTTPServer.SimpleHTTPRequestHandler.do_HEAD"> <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> <dd><p>This method serves the <tt class="docutils literal"><span class="pre">'HEAD'</span></tt> request type: it sends the headers it 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> method for a more complete explanation of the possible headers.</p> </dd></dl> <dl class="method"> <dt id="SimpleHTTPServer.SimpleHTTPRequestHandler.do_GET"> <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> <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 <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 file’s contents are returned; otherwise a directory listing is generated by calling the <tt class="xref py py-meth docutils literal"><span class="pre">list_directory()</span></tt> method. This method uses <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 response if the <tt class="xref py py-func docutils literal"><span class="pre">listdir()</span></tt> 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"><tt class="xref py py-exc docutils literal"><span class="pre">IOError</span></tt></a> exception in opening the requested file is 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 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 uses the <em>extensions_map</em> variable.</p> <p>A <tt class="docutils literal"><span class="pre">'Content-type:'</span></tt> header with the guessed content type is output, followed by a <tt class="docutils literal"><span class="pre">'Content-Length:'</span></tt> header with the file’s size and a <tt class="docutils literal"><span class="pre">'Last-Modified:'</span></tt> 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 <tt class="docutils literal"><span class="pre">text/</span></tt> 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."><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 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> as the Handler.</p> <p class="versionadded"> <span class="versionmodified">New in version 2.5: </span>The <tt class="docutils literal"><span class="pre">'Last-Modified'</span></tt> header.</p> </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."><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 to set up a very basic web server serving files relative to the current directory.</p> <div class="highlight-python"><div class="highlight"><pre><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="s">""</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="k">print</span> <span class="s">"serving at port"</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."><tt class="xref py py-mod docutils literal"><span class="pre">SimpleHTTPServer</span></tt></a> module can also be invoked directly using the <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. Similar to the previous example, this serves the files relative to the current directory.</p> <div class="highlight-python"><pre>python -m SimpleHTTPServer 8000</pre> </div> <div class="admonition-see-also admonition seealso"> <p class="first admonition-title">See also</p> <dl class="last docutils"> <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> <dd>Base class implementation for Web server and request handler.</dd> </dl> </div> </div> </div> </div> </div> <div class="sphinxsidebar"> <div class="sphinxsidebarwrapper"> <h4>Previous topic</h4> <p class="topless"><a href="basehttpserver.html" title="previous chapter">20.18. <tt class="docutils literal"><span class="pre">BaseHTTPServer</span></tt> — Basic HTTP server</a></p> <h4>Next topic</h4> <p class="topless"><a href="cgihttpserver.html" title="next chapter">20.20. <tt class="docutils literal"><span class="pre">CGIHTTPServer</span></tt> — CGI-capable HTTP request handler</a></p> <h3>This Page</h3> <ul class="this-page-menu"> <li><a href="../bugs.html">Report a Bug</a></li> <li><a href="../_sources/library/simplehttpserver.txt" rel="nofollow">Show Source</a></li> </ul> <div id="searchbox" style="display: none"> <h3>Quick search</h3> <form class="search" action="../search.html" method="get"> <input type="text" name="q" /> <input type="submit" value="Go" /> <input type="hidden" name="check_keywords" value="yes" /> <input type="hidden" name="area" value="default" /> </form> <p class="searchtip" style="font-size: 90%"> Enter search terms or a module, class or function name. </p> </div> <script type="text/javascript">$('#searchbox').show(0);</script> </div> </div> <div class="clearer"></div> </div> <div class="related"> <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="http://www.python.org/">Python</a> »</li> <li> <a href="../index.html">Python 2.7.5 documentation</a> » </li> <li><a href="index.html" >The Python Standard Library</a> »</li> <li><a href="internet.html" >20. Internet Protocols and Support</a> »</li> </ul> </div> <div class="footer"> © <a href="../copyright.html">Copyright</a> 1990-2019, Python Software Foundation. <br /> The Python Software Foundation is a non-profit corporation. <a href="http://www.python.org/psf/donations/">Please donate.</a> <br /> Last updated on Jul 03, 2019. <a href="../bugs.html">Found a bug</a>? <br /> Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3. </div> </body> </html>