parthtamu/rag-code-assistant
0
1<!DOCTYPE html>2 3<html lang="en" data-content_root="../">4 <head>5 <meta charset="utf-8" />6 <meta name="viewport" content="width=device-width, initial-scale=1.0" /><meta name="viewport" content="width=device-width, initial-scale=1" />7<meta property="og:title" content="http.server — HTTP servers" />8<meta property="og:type" content="website" />9<meta property="og:url" content="https://docs.python.org/3/library/http.server.html" />10<meta property="og:site_name" content="Python documentation" />11<meta property="og:description" content="Source code: Lib/http/server.py This module defines classes for implementing HTTP servers. Availability: not WASI. This module does not work or is not available on WebAssembly. See WebAssembly plat..." />12<meta property="og:image:width" content="1146" />13<meta property="og:image:height" content="600" />14<meta property="og:image" content="https://docs.python.org/3.15/_images/social_previews/summary_library_http.server_3073e72c.png" />15<meta property="og:image:alt" content="Source code: Lib/http/server.py This module defines classes for implementing HTTP servers. Availability: not WASI. This module does not work or is not available on WebAssembly. See WebAssembly plat..." />16<meta name="description" content="Source code: Lib/http/server.py This module defines classes for implementing HTTP servers. Availability: not WASI. This module does not work or is not available on WebAssembly. See WebAssembly plat..." />17<meta name="twitter:card" content="summary_large_image" />18<meta name="theme-color" content="#3776ab">19 20 <title>http.server — HTTP servers — Python 3.15.0a6 documentation</title><meta name="viewport" content="width=device-width, initial-scale=1.0">21 22 <link rel="stylesheet" type="text/css" href="../_static/pygments.css?v=b86133f3" />23 <link rel="stylesheet" type="text/css" href="../_static/classic.css?v=234b1a7c" />24 <link rel="stylesheet" type="text/css" href="../_static/pydoctheme.css?v=89a2f22a" />25 <link rel="stylesheet" type="text/css" href="../_static/profiling-sampling-visualization.css?v=0c2600ae" />26 <link id="pygments_dark_css" media="(prefers-color-scheme: dark)" rel="stylesheet" type="text/css" href="../_static/pygments_dark.css?v=5349f25f" />27 28 <script src="../_static/documentation_options.js?v=6b7c9ff5"></script>29 <script src="../_static/doctools.js?v=9bcbadda"></script>30 <script src="../_static/sphinx_highlight.js?v=dc90522c"></script>31 <script src="../_static/profiling-sampling-visualization.js?v=9811ed04"></script>32 33 <script src="../_static/sidebar.js"></script>34 35 <link rel="search" type="application/opensearchdescription+xml"36 title="Search within Python 3.15.0a6 documentation"37 href="../_static/opensearch.xml"/>38 <link rel="author" title="About these documents" href="../about.html" />39 <link rel="index" title="Index" href="../genindex.html" />40 <link rel="search" title="Search" href="../search.html" />41 <link rel="copyright" title="Copyright" href="../copyright.html" />42 <link rel="next" title="http.cookies — HTTP state management" href="http.cookies.html" />43 <link rel="prev" title="socketserver — A framework for network servers" href="socketserver.html" />44 45 46 <script defer file-types="bz2,epub,zip" data-domain="docs.python.org" src="https://analytics.python.org/js/script.file-downloads.outbound-links.js"></script>47 48 <link rel="canonical" href="https://docs.python.org/3/library/http.server.html">49 50 51 52 53 <style>54 @media only screen {55 table.full-width-table {56 width: 100%;57 }58 }59 </style>60<link rel="stylesheet" href="../_static/pydoctheme_dark.css" media="(prefers-color-scheme: dark)" id="pydoctheme_dark_css">61 <link rel="shortcut icon" type="image/png" href="../_static/py.svg">62 <script type="text/javascript" src="../_static/copybutton.js"></script>63 <script type="text/javascript" src="../_static/menu.js"></script>64 <script type="text/javascript" src="../_static/search-focus.js"></script>65 <script type="text/javascript" src="../_static/themetoggle.js"></script> 66 <script type="text/javascript" src="../_static/rtd_switcher.js"></script>67 <meta name="readthedocs-addons-api-version" content="1">68 69 </head>70<body>71<div class="mobile-nav">72 <input type="checkbox" id="menuToggler" class="toggler__input" aria-controls="navigation"73 aria-pressed="false" aria-expanded="false" role="button" aria-label="Menu">74 <nav class="nav-content" role="navigation">75 <label for="menuToggler" class="toggler__label">76 <span></span>77 </label>78 <span class="nav-items-wrapper">79 <a href="https://www.python.org/" class="nav-logo">80 <img src="../_static/py.svg" alt="Python logo">81 </a>82 <span class="version_switcher_placeholder"></span>83 <form role="search" class="search" action="../search.html" method="get">84 <svg xmlns="http://www.w3.org/2000/svg" width="20" height="20" viewBox="0 0 24 24" class="search-icon">85 <path fill-rule="nonzero" fill="currentColor" d="M15.5 14h-.79l-.28-.27a6.5 6.5 0 001.48-5.34c-.47-2.78-2.79-5-5.59-5.34a6.505 6.505 0 00-7.27 7.27c.34 2.8 2.56 5.12 5.34 5.59a6.5 6.5 0 005.34-1.48l.27.28v.79l4.25 4.25c.41.41 1.08.41 1.49 0 .41-.41.41-1.08 0-1.49L15.5 14zm-6 0C7.01 14 5 11.99 5 9.5S7.01 5 9.5 5 14 7.01 14 9.5 11.99 14 9.5 14z"></path>86 </svg>87 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q">88 <input type="submit" value="Go">89 </form>90 </span>91 </nav>92 <div class="menu-wrapper">93 <nav class="menu" role="navigation" aria-label="main navigation">94 <div class="language_switcher_placeholder"></div>95 96<label class="theme-selector-label">97 Theme98 <select class="theme-selector" oninput="activateTheme(this.value)">99 <option value="auto" selected>Auto</option>100 <option value="light">Light</option>101 <option value="dark">Dark</option>102 </select>103</label>104 <div>105 <h3><a href="../contents.html">Table of Contents</a></h3>106 <ul>107<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.server</span></code> — HTTP servers</a><ul>108<li><a class="reference internal" href="#command-line-interface">Command-line interface</a></li>109<li><a class="reference internal" href="#security-considerations">Security considerations</a></li>110</ul>111</li>112</ul>113 114 </div>115 <div>116 <h4>Previous topic</h4>117 <p class="topless"><a href="socketserver.html"118 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">socketserver</span></code> — A framework for network servers</a></p>119 </div>120 <div>121 <h4>Next topic</h4>122 <p class="topless"><a href="http.cookies.html"123 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.cookies</span></code> — HTTP state management</a></p>124 </div>125 <script>126 document.addEventListener('DOMContentLoaded', () => {127 const title = document.querySelector('meta[property="og:title"]').content;128 const elements = document.querySelectorAll('.improvepage');129 const pageurl = window.location.href.split('?')[0];130 elements.forEach(element => {131 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));132 url.searchParams.set('pagetitle', title);133 url.searchParams.set('pageurl', pageurl);134 url.searchParams.set('pagesource', "library/http.server.rst");135 element.href = url.toString();136 });137 });138 </script>139 <div role="note" aria-label="source link">140 <h3>This page</h3>141 <ul class="this-page-menu">142 <li><a href="../bugs.html">Report a bug</a></li>143 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>144 <li>145 <a href="https://github.com/python/cpython/blob/main/Doc/library/http.server.rst?plain=1"146 rel="nofollow">Show source147 </a>148 </li>149 150 </ul>151 </div>152 </nav>153 </div>154</div>155 156 157 <div class="related" role="navigation" aria-label="Related">158 <h3>Navigation</h3>159 <ul>160 <li class="right" style="margin-right: 10px">161 <a href="../genindex.html" title="General Index"162 accesskey="I">index</a></li>163 <li class="right" >164 <a href="../py-modindex.html" title="Python Module Index"165 >modules</a> |</li>166 <li class="right" >167 <a href="http.cookies.html" title="http.cookies — HTTP state management"168 accesskey="N">next</a> |</li>169 <li class="right" >170 <a href="socketserver.html" title="socketserver — A framework for network servers"171 accesskey="P">previous</a> |</li>172 173 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>174 <li><a href="https://www.python.org/">Python</a> »</li>175 <li class="switchers">176 <div class="language_switcher_placeholder"></div>177 <div class="version_switcher_placeholder"></div>178 </li>179 <li>180 181 </li>182 <li id="cpython-language-and-version">183 <a href="../index.html">3.15.0a6 Documentation</a> »184 </li>185 186 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>187 <li class="nav-item nav-item-2"><a href="internet.html" accesskey="U">Internet Protocols and Support</a> »</li>188 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.server</span></code> — HTTP servers</a></li>189 <li class="right">190 191 192 <div class="inline-search" role="search">193 <form class="inline-search" action="../search.html" method="get">194 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">195 <input type="submit" value="Go">196 </form>197 </div>198 |199 </li>200 <li class="right">201<label class="theme-selector-label">202 Theme203 <select class="theme-selector" oninput="activateTheme(this.value)">204 <option value="auto" selected>Auto</option>205 <option value="light">Light</option>206 <option value="dark">Dark</option>207 </select>208</label> |</li>209 210 </ul>211 </div> 212 213 <div class="document">214 <div class="documentwrapper">215 <div class="bodywrapper">216 <div class="body" role="main">217 218 <section id="module-http.server">219<span id="http-server-http-servers"></span><h1><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.server</span></code> — HTTP servers<a class="headerlink" href="#module-http.server" title="Link to this heading">¶</a></h1>220<p><strong>Source code:</strong> <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/http/server.py">Lib/http/server.py</a></p>221<hr class="docutils" id="index-0" />222<p>This module defines classes for implementing HTTP servers.</p>223<div class="admonition warning">224<p class="admonition-title">Warning</p>225<p><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.server</span></code> is not recommended for production. It only implements226<a class="reference internal" href="#http-server-security"><span class="std std-ref">basic security checks</span></a>.</p>227</div>228<div class="availability docutils container">229<p><a class="reference internal" href="intro.html#availability"><span class="std std-ref">Availability</span></a>: not WASI.</p>230<p>This module does not work or is not available on WebAssembly. See231<a class="reference internal" href="intro.html#wasm-availability"><span class="std std-ref">WebAssembly platforms</span></a> for more information.</p>232</div>233<p>One class, <a class="reference internal" href="#http.server.HTTPServer" title="http.server.HTTPServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPServer</span></code></a>, is a <a class="reference internal" href="socketserver.html#socketserver.TCPServer" title="socketserver.TCPServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">socketserver.TCPServer</span></code></a> subclass.234It creates and listens at the HTTP socket, dispatching the requests to a235handler. Code to create and run the server looks like this:</p>236<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="k">def</span><span class="w"> </span><span class="nf">run</span><span class="p">(</span><span class="n">server_class</span><span class="o">=</span><span class="n">HTTPServer</span><span class="p">,</span> <span class="n">handler_class</span><span class="o">=</span><span class="n">BaseHTTPRequestHandler</span><span class="p">):</span>237 <span class="n">server_address</span> <span class="o">=</span> <span class="p">(</span><span class="s1">''</span><span class="p">,</span> <span class="mi">8000</span><span class="p">)</span>238 <span class="n">httpd</span> <span class="o">=</span> <span class="n">server_class</span><span class="p">(</span><span class="n">server_address</span><span class="p">,</span> <span class="n">handler_class</span><span class="p">)</span>239 <span class="n">httpd</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>240</pre></div>241</div>242<dl class="py class">243<dt class="sig sig-object py" id="http.server.HTTPServer">244<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">http.server.</span></span><span class="sig-name descname"><span class="pre">HTTPServer</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">server_address</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">RequestHandlerClass</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#http.server.HTTPServer" title="Link to this definition">¶</a></dt>245<dd><p>This class builds on the <a class="reference internal" href="socketserver.html#socketserver.TCPServer" title="socketserver.TCPServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">TCPServer</span></code></a> class by storing246the server address as instance variables named <code class="xref py py-attr docutils literal notranslate"><span class="pre">server_name</span></code> and247<code class="xref py py-attr docutils literal notranslate"><span class="pre">server_port</span></code>. The server is accessible by the handler, typically248through the handler’s <code class="xref py py-attr docutils literal notranslate"><span class="pre">server</span></code> instance variable.</p>249</dd></dl>250 251<dl class="py class">252<dt class="sig sig-object py" id="http.server.ThreadingHTTPServer">253<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">http.server.</span></span><span class="sig-name descname"><span class="pre">ThreadingHTTPServer</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">server_address</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">RequestHandlerClass</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#http.server.ThreadingHTTPServer" title="Link to this definition">¶</a></dt>254<dd><p>This class is identical to HTTPServer but uses threads to handle255requests by using the <a class="reference internal" href="socketserver.html#socketserver.ThreadingMixIn" title="socketserver.ThreadingMixIn"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadingMixIn</span></code></a>. This256is useful to handle web browsers pre-opening sockets, on which257<a class="reference internal" href="#http.server.HTTPServer" title="http.server.HTTPServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPServer</span></code></a> would wait indefinitely.</p>258<div class="versionadded">259<p><span class="versionmodified added">Added in version 3.7.</span></p>260</div>261</dd></dl>262 263<dl class="py class">264<dt class="sig sig-object py" id="http.server.HTTPSServer">265<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">http.server.</span></span><span class="sig-name descname"><span class="pre">HTTPSServer</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">server_address</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">RequestHandlerClass</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">bind_and_activate</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">certfile</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">keyfile</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">password</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">alpn_protocols</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#http.server.HTTPSServer" title="Link to this definition">¶</a></dt>266<dd><p>Subclass of <a class="reference internal" href="#http.server.HTTPServer" title="http.server.HTTPServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPServer</span></code></a> with a wrapped socket using the <a class="reference internal" href="ssl.html#module-ssl" title="ssl: TLS/SSL wrapper for socket objects"><code class="xref py py-mod docutils literal notranslate"><span class="pre">ssl</span></code></a> module.267If the <code class="xref py py-mod docutils literal notranslate"><span class="pre">ssl</span></code> module is not available, instantiating a <code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPSServer</span></code>268object fails with a <a class="reference internal" href="exceptions.html#RuntimeError" title="RuntimeError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">RuntimeError</span></code></a>.</p>269<p>The <em>certfile</em> argument is the path to the SSL certificate chain file,270and the <em>keyfile</em> is the path to file containing the private key.</p>271<p>A <em>password</em> can be specified for files protected and wrapped with PKCS#8,272but beware that this could possibly expose hardcoded passwords in clear.</p>273<div class="admonition seealso">274<p class="admonition-title">See also</p>275<p>See <a class="reference internal" href="ssl.html#ssl.SSLContext.load_cert_chain" title="ssl.SSLContext.load_cert_chain"><code class="xref py py-meth docutils literal notranslate"><span class="pre">ssl.SSLContext.load_cert_chain()</span></code></a> for additional276information on the accepted values for <em>certfile</em>, <em>keyfile</em>277and <em>password</em>.</p>278</div>279<p>When specified, the <em>alpn_protocols</em> argument must be a sequence of strings280specifying the “Application-Layer Protocol Negotiation” (ALPN) protocols281supported by the server. ALPN allows the server and the client to negotiate282the application protocol during the TLS handshake.</p>283<p>By default, it is set to <code class="docutils literal notranslate"><span class="pre">["http/1.1"]</span></code>, meaning the server supports HTTP/1.1.</p>284<div class="versionadded">285<p><span class="versionmodified added">Added in version 3.14.</span></p>286</div>287</dd></dl>288 289<dl class="py class">290<dt class="sig sig-object py" id="http.server.ThreadingHTTPSServer">291<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">http.server.</span></span><span class="sig-name descname"><span class="pre">ThreadingHTTPSServer</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">server_address</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">RequestHandlerClass</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">bind_and_activate</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">True</span></span></em>, <em class="sig-param"><span class="keyword-only-separator o"><abbr title="Keyword-only parameters separator (PEP 3102)"><span class="pre">*</span></abbr></span></em>, <em class="sig-param"><span class="n"><span class="pre">certfile</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">keyfile</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">password</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">alpn_protocols</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#http.server.ThreadingHTTPSServer" title="Link to this definition">¶</a></dt>292<dd><p>This class is identical to <a class="reference internal" href="#http.server.HTTPSServer" title="http.server.HTTPSServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPSServer</span></code></a> but uses threads to handle293requests by inheriting from <a class="reference internal" href="socketserver.html#socketserver.ThreadingMixIn" title="socketserver.ThreadingMixIn"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadingMixIn</span></code></a>. This is294analogous to <a class="reference internal" href="#http.server.ThreadingHTTPServer" title="http.server.ThreadingHTTPServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadingHTTPServer</span></code></a> only using <code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPSServer</span></code>.</p>295<div class="versionadded">296<p><span class="versionmodified added">Added in version 3.14.</span></p>297</div>298</dd></dl>299 300<p>The <a class="reference internal" href="#http.server.HTTPServer" title="http.server.HTTPServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPServer</span></code></a>, <a class="reference internal" href="#http.server.ThreadingHTTPServer" title="http.server.ThreadingHTTPServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadingHTTPServer</span></code></a>, <a class="reference internal" href="#http.server.HTTPSServer" title="http.server.HTTPSServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">HTTPSServer</span></code></a> and301<a class="reference internal" href="#http.server.ThreadingHTTPSServer" title="http.server.ThreadingHTTPSServer"><code class="xref py py-class docutils literal notranslate"><span class="pre">ThreadingHTTPSServer</span></code></a> must be given a <em>RequestHandlerClass</em> on302instantiation, of which this module provides three different variants:</p>303<dl class="py class">304<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler">305<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">http.server.</span></span><span class="sig-name descname"><span class="pre">BaseHTTPRequestHandler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">request</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">client_address</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">server</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler" title="Link to this definition">¶</a></dt>306<dd><p>This class is used to handle the HTTP requests that arrive at the server. By307itself, it cannot respond to any actual HTTP requests; it must be subclassed308to handle each request method (e.g. GET or POST).309<code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHTTPRequestHandler</span></code> provides a number of class and instance310variables, and methods for use by subclasses.</p>311<p>The handler will parse the request and the headers, then call a method312specific to the request type. The method name is constructed from the313request. For example, for the request method <code class="docutils literal notranslate"><span class="pre">SPAM</span></code>, the <code class="xref py py-meth docutils literal notranslate"><span class="pre">do_SPAM()</span></code>314method will be called with no arguments. All of the relevant information is315stored in instance variables of the handler. Subclasses should not need to316override or extend the <code class="xref py py-meth docutils literal notranslate"><span class="pre">__init__()</span></code> method.</p>317<p><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHTTPRequestHandler</span></code> has the following instance variables:</p>318<dl class="py attribute">319<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.client_address">320<span class="sig-name descname"><span class="pre">client_address</span></span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.client_address" title="Link to this definition">¶</a></dt>321<dd><p>Contains a tuple of the form <code class="docutils literal notranslate"><span class="pre">(host,</span> <span class="pre">port)</span></code> referring to the client’s322address.</p>323</dd></dl>324 325<dl class="py attribute">326<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.server">327<span class="sig-name descname"><span class="pre">server</span></span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.server" title="Link to this definition">¶</a></dt>328<dd><p>Contains the server instance.</p>329</dd></dl>330 331<dl class="py attribute">332<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.close_connection">333<span class="sig-name descname"><span class="pre">close_connection</span></span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.close_connection" title="Link to this definition">¶</a></dt>334<dd><p>Boolean that should be set before <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.handle_one_request" title="http.server.BaseHTTPRequestHandler.handle_one_request"><code class="xref py py-meth docutils literal notranslate"><span class="pre">handle_one_request()</span></code></a> returns,335indicating if another request may be expected, or if the connection should336be shut down.</p>337</dd></dl>338 339<dl class="py attribute">340<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.requestline">341<span class="sig-name descname"><span class="pre">requestline</span></span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.requestline" title="Link to this definition">¶</a></dt>342<dd><p>Contains the string representation of the HTTP request line. The343terminating CRLF is stripped. This attribute should be set by344<a class="reference internal" href="#http.server.BaseHTTPRequestHandler.handle_one_request" title="http.server.BaseHTTPRequestHandler.handle_one_request"><code class="xref py py-meth docutils literal notranslate"><span class="pre">handle_one_request()</span></code></a>. If no valid request line was processed, it345should be set to the empty string.</p>346</dd></dl>347 348<dl class="py attribute">349<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.command">350<span class="sig-name descname"><span class="pre">command</span></span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.command" title="Link to this definition">¶</a></dt>351<dd><p>Contains the command (request type). For example, <code class="docutils literal notranslate"><span class="pre">'GET'</span></code>.</p>352</dd></dl>353 354<dl class="py attribute">355<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.path">356<span class="sig-name descname"><span class="pre">path</span></span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.path" title="Link to this definition">¶</a></dt>357<dd><p>Contains the request path. If query component of the URL is present,358then <code class="docutils literal notranslate"><span class="pre">path</span></code> includes the query. Using the terminology of <span class="target" id="index-1"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc3986.html"><strong>RFC 3986</strong></a>,359<code class="docutils literal notranslate"><span class="pre">path</span></code> here includes <code class="docutils literal notranslate"><span class="pre">hier-part</span></code> and the <code class="docutils literal notranslate"><span class="pre">query</span></code>.</p>360</dd></dl>361 362<dl class="py attribute">363<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.request_version">364<span class="sig-name descname"><span class="pre">request_version</span></span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.request_version" title="Link to this definition">¶</a></dt>365<dd><p>Contains the version string from the request. For example, <code class="docutils literal notranslate"><span class="pre">'HTTP/1.0'</span></code>.</p>366</dd></dl>367 368<dl class="py attribute">369<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.headers">370<span class="sig-name descname"><span class="pre">headers</span></span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.headers" title="Link to this definition">¶</a></dt>371<dd><p>Holds an instance of the class specified by the <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.MessageClass" title="http.server.BaseHTTPRequestHandler.MessageClass"><code class="xref py py-attr docutils literal notranslate"><span class="pre">MessageClass</span></code></a> class372variable. This instance parses and manages the headers in the HTTP373request. The <a class="reference internal" href="http.client.html#http.client.parse_headers" title="http.client.parse_headers"><code class="xref py py-func docutils literal notranslate"><span class="pre">parse_headers()</span></code></a> function from374<a class="reference internal" href="http.client.html#module-http.client" title="http.client: HTTP and HTTPS protocol client (requires sockets)."><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.client</span></code></a> is used to parse the headers and it requires that the375HTTP request provide a valid <span class="target" id="index-2"></span><a class="rfc reference external" href="https://datatracker.ietf.org/doc/html/rfc5322.html"><strong>RFC 5322</strong></a> style header.</p>376</dd></dl>377 378<dl class="py attribute">379<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.rfile">380<span class="sig-name descname"><span class="pre">rfile</span></span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.rfile" title="Link to this definition">¶</a></dt>381<dd><p>An <a class="reference internal" href="io.html#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BufferedIOBase</span></code></a> input stream, ready to read from382the start of the optional input data.</p>383</dd></dl>384 385<dl class="py attribute">386<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.wfile">387<span class="sig-name descname"><span class="pre">wfile</span></span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.wfile" title="Link to this definition">¶</a></dt>388<dd><p>Contains the output stream for writing a response back to the389client. Proper adherence to the HTTP protocol must be used when writing to390this stream in order to achieve successful interoperation with HTTP391clients.</p>392<div class="versionchanged">393<p><span class="versionmodified changed">Changed in version 3.6: </span>This is an <a class="reference internal" href="io.html#io.BufferedIOBase" title="io.BufferedIOBase"><code class="xref py py-class docutils literal notranslate"><span class="pre">io.BufferedIOBase</span></code></a> stream.</p>394</div>395</dd></dl>396 397<p><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHTTPRequestHandler</span></code> has the following attributes:</p>398<dl class="py attribute">399<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.server_version">400<span class="sig-name descname"><span class="pre">server_version</span></span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.server_version" title="Link to this definition">¶</a></dt>401<dd><p>Specifies the server software version. You may want to override this. The402format is multiple whitespace-separated strings, where each string is of403the form name[/version]. For example, <code class="docutils literal notranslate"><span class="pre">'BaseHTTP/0.2'</span></code>.</p>404</dd></dl>405 406<dl class="py attribute">407<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.sys_version">408<span class="sig-name descname"><span class="pre">sys_version</span></span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.sys_version" title="Link to this definition">¶</a></dt>409<dd><p>Contains the Python system version, in a form usable by the410<a class="reference internal" href="#http.server.BaseHTTPRequestHandler.version_string" title="http.server.BaseHTTPRequestHandler.version_string"><code class="xref py py-attr docutils literal notranslate"><span class="pre">version_string</span></code></a> method and the <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.server_version" title="http.server.BaseHTTPRequestHandler.server_version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">server_version</span></code></a> class411variable. For example, <code class="docutils literal notranslate"><span class="pre">'Python/1.4'</span></code>.</p>412</dd></dl>413 414<dl class="py attribute">415<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.error_message_format">416<span class="sig-name descname"><span class="pre">error_message_format</span></span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.error_message_format" title="Link to this definition">¶</a></dt>417<dd><p>Specifies a format string that should be used by <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.send_error" title="http.server.BaseHTTPRequestHandler.send_error"><code class="xref py py-meth docutils literal notranslate"><span class="pre">send_error()</span></code></a> method418for building an error response to the client. The string is filled by419default with variables from <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.responses" title="http.server.BaseHTTPRequestHandler.responses"><code class="xref py py-attr docutils literal notranslate"><span class="pre">responses</span></code></a> based on the status code420that passed to <code class="xref py py-meth docutils literal notranslate"><span class="pre">send_error()</span></code>.</p>421</dd></dl>422 423<dl class="py attribute">424<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.error_content_type">425<span class="sig-name descname"><span class="pre">error_content_type</span></span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.error_content_type" title="Link to this definition">¶</a></dt>426<dd><p>Specifies the Content-Type HTTP header of error responses sent to the427client. The default value is <code class="docutils literal notranslate"><span class="pre">'text/html'</span></code>.</p>428</dd></dl>429 430<dl class="py attribute">431<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.protocol_version">432<span class="sig-name descname"><span class="pre">protocol_version</span></span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.protocol_version" title="Link to this definition">¶</a></dt>433<dd><p>Specifies the HTTP version to which the server is conformant. It is sent434in responses to let the client know the server’s communication435capabilities for future requests. If set to436<code class="docutils literal notranslate"><span class="pre">'HTTP/1.1'</span></code>, the server will permit HTTP persistent connections;437however, your server <em>must</em> then include an accurate <code class="docutils literal notranslate"><span class="pre">Content-Length</span></code>438header (using <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.send_header" title="http.server.BaseHTTPRequestHandler.send_header"><code class="xref py py-meth docutils literal notranslate"><span class="pre">send_header()</span></code></a>) in all of its responses to clients.439For backwards compatibility, the setting defaults to <code class="docutils literal notranslate"><span class="pre">'HTTP/1.0'</span></code>.</p>440</dd></dl>441 442<dl class="py attribute">443<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.MessageClass">444<span class="sig-name descname"><span class="pre">MessageClass</span></span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.MessageClass" title="Link to this definition">¶</a></dt>445<dd><p>Specifies an <a class="reference internal" href="email.compat32-message.html#email.message.Message" title="email.message.Message"><code class="xref py py-class docutils literal notranslate"><span class="pre">email.message.Message</span></code></a>-like class to parse HTTP446headers. Typically, this is not overridden, and it defaults to447<a class="reference internal" href="http.client.html#http.client.HTTPMessage" title="http.client.HTTPMessage"><code class="xref py py-class docutils literal notranslate"><span class="pre">http.client.HTTPMessage</span></code></a>.</p>448</dd></dl>449 450<dl class="py attribute">451<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.responses">452<span class="sig-name descname"><span class="pre">responses</span></span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.responses" title="Link to this definition">¶</a></dt>453<dd><p>This attribute contains a mapping of error code integers to two-element tuples454containing a short and long message. For example, <code class="docutils literal notranslate"><span class="pre">{code:</span> <span class="pre">(shortmessage,</span>455<span class="pre">longmessage)}</span></code>. The <em>shortmessage</em> is usually used as the <em>message</em> key in an456error response, and <em>longmessage</em> as the <em>explain</em> key. It is used by457<a class="reference internal" href="#http.server.BaseHTTPRequestHandler.send_response_only" title="http.server.BaseHTTPRequestHandler.send_response_only"><code class="xref py py-meth docutils literal notranslate"><span class="pre">send_response_only()</span></code></a> and <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.send_error" title="http.server.BaseHTTPRequestHandler.send_error"><code class="xref py py-meth docutils literal notranslate"><span class="pre">send_error()</span></code></a> methods.</p>458</dd></dl>459 460<p>A <code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHTTPRequestHandler</span></code> instance has the following methods:</p>461<dl class="py method">462<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.handle">463<span class="sig-name descname"><span class="pre">handle</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.handle" title="Link to this definition">¶</a></dt>464<dd><p>Calls <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.handle_one_request" title="http.server.BaseHTTPRequestHandler.handle_one_request"><code class="xref py py-meth docutils literal notranslate"><span class="pre">handle_one_request()</span></code></a> once (or, if persistent connections are465enabled, multiple times) to handle incoming HTTP requests. You should466never need to override it; instead, implement appropriate <code class="xref py py-meth docutils literal notranslate"><span class="pre">do_*()</span></code>467methods.</p>468</dd></dl>469 470<dl class="py method">471<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.handle_one_request">472<span class="sig-name descname"><span class="pre">handle_one_request</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.handle_one_request" title="Link to this definition">¶</a></dt>473<dd><p>This method will parse and dispatch the request to the appropriate474<code class="xref py py-meth docutils literal notranslate"><span class="pre">do_*()</span></code> method. You should never need to override it.</p>475</dd></dl>476 477<dl class="py method">478<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.handle_expect_100">479<span class="sig-name descname"><span class="pre">handle_expect_100</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.handle_expect_100" title="Link to this definition">¶</a></dt>480<dd><p>When an HTTP/1.1 conformant server receives an <code class="docutils literal notranslate"><span class="pre">Expect:</span> <span class="pre">100-continue</span></code>481request header it responds back with a <code class="docutils literal notranslate"><span class="pre">100</span> <span class="pre">Continue</span></code> followed by <code class="docutils literal notranslate"><span class="pre">200</span>482<span class="pre">OK</span></code> headers.483This method can be overridden to raise an error if the server does not484want the client to continue. For e.g. server can choose to send <code class="docutils literal notranslate"><span class="pre">417</span>485<span class="pre">Expectation</span> <span class="pre">Failed</span></code> as a response header and <code class="docutils literal notranslate"><span class="pre">return</span> <span class="pre">False</span></code>.</p>486<div class="versionadded">487<p><span class="versionmodified added">Added in version 3.2.</span></p>488</div>489</dd></dl>490 491<dl class="py method">492<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.send_error">493<span class="sig-name descname"><span class="pre">send_error</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">code</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">message</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">explain</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.send_error" title="Link to this definition">¶</a></dt>494<dd><p>Sends and logs a complete error reply to the client. The numeric <em>code</em>495specifies the HTTP error code, with <em>message</em> as an optional, short, human496readable description of the error. The <em>explain</em> argument can be used to497provide more detailed information about the error; it will be formatted498using the <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.error_message_format" title="http.server.BaseHTTPRequestHandler.error_message_format"><code class="xref py py-attr docutils literal notranslate"><span class="pre">error_message_format</span></code></a> attribute and emitted, after499a complete set of headers, as the response body. The <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.responses" title="http.server.BaseHTTPRequestHandler.responses"><code class="xref py py-attr docutils literal notranslate"><span class="pre">responses</span></code></a>500attribute holds the default values for <em>message</em> and <em>explain</em> that501will be used if no value is provided; for unknown codes the default value502for both is the string <code class="docutils literal notranslate"><span class="pre">???</span></code>. The body will be empty if the method is503HEAD or the response code is one of the following: <code class="samp docutils literal notranslate"><span class="pre">1</span><em><span class="pre">xx</span></em></code>,504<code class="docutils literal notranslate"><span class="pre">204</span> <span class="pre">No</span> <span class="pre">Content</span></code>, <code class="docutils literal notranslate"><span class="pre">205</span> <span class="pre">Reset</span> <span class="pre">Content</span></code>, <code class="docutils literal notranslate"><span class="pre">304</span> <span class="pre">Not</span> <span class="pre">Modified</span></code>.</p>505<div class="versionchanged">506<p><span class="versionmodified changed">Changed in version 3.4: </span>The error response includes a Content-Length header.507Added the <em>explain</em> argument.</p>508</div>509</dd></dl>510 511<dl class="py method">512<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.send_response">513<span class="sig-name descname"><span class="pre">send_response</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">code</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">message</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.send_response" title="Link to this definition">¶</a></dt>514<dd><p>Adds a response header to the headers buffer and logs the accepted515request. The HTTP response line is written to the internal buffer,516followed by <em>Server</em> and <em>Date</em> headers. The values for these two headers517are picked up from the <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.version_string" title="http.server.BaseHTTPRequestHandler.version_string"><code class="xref py py-meth docutils literal notranslate"><span class="pre">version_string()</span></code></a> and518<a class="reference internal" href="#http.server.BaseHTTPRequestHandler.date_time_string" title="http.server.BaseHTTPRequestHandler.date_time_string"><code class="xref py py-meth docutils literal notranslate"><span class="pre">date_time_string()</span></code></a> methods, respectively. If the server does not519intend to send any other headers using the <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.send_header" title="http.server.BaseHTTPRequestHandler.send_header"><code class="xref py py-meth docutils literal notranslate"><span class="pre">send_header()</span></code></a> method,520then <code class="xref py py-meth docutils literal notranslate"><span class="pre">send_response()</span></code> should be followed by an <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.end_headers" title="http.server.BaseHTTPRequestHandler.end_headers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">end_headers()</span></code></a>521call.</p>522<div class="versionchanged">523<p><span class="versionmodified changed">Changed in version 3.3: </span>Headers are stored to an internal buffer and <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.end_headers" title="http.server.BaseHTTPRequestHandler.end_headers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">end_headers()</span></code></a>524needs to be called explicitly.</p>525</div>526</dd></dl>527 528<dl class="py method">529<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.send_header">530<span class="sig-name descname"><span class="pre">send_header</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">keyword</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">value</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.send_header" title="Link to this definition">¶</a></dt>531<dd><p>Adds the HTTP header to an internal buffer which will be written to the532output stream when either <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.end_headers" title="http.server.BaseHTTPRequestHandler.end_headers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">end_headers()</span></code></a> or <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.flush_headers" title="http.server.BaseHTTPRequestHandler.flush_headers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush_headers()</span></code></a> is533invoked. <em>keyword</em> should specify the header keyword, with <em>value</em>534specifying its value. Note that, after the send_header calls are done,535<code class="xref py py-meth docutils literal notranslate"><span class="pre">end_headers()</span></code> MUST BE called in order to complete the operation.</p>536<div class="versionchanged">537<p><span class="versionmodified changed">Changed in version 3.2: </span>Headers are stored in an internal buffer.</p>538</div>539</dd></dl>540 541<dl class="py method">542<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.send_response_only">543<span class="sig-name descname"><span class="pre">send_response_only</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">code</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">message</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.send_response_only" title="Link to this definition">¶</a></dt>544<dd><p>Sends the response header only, used for the purposes when <code class="docutils literal notranslate"><span class="pre">100</span>545<span class="pre">Continue</span></code> response is sent by the server to the client. The headers not546buffered and sent directly the output stream.If the <em>message</em> is not547specified, the HTTP message corresponding the response <em>code</em> is sent.</p>548<div class="versionadded">549<p><span class="versionmodified added">Added in version 3.2.</span></p>550</div>551</dd></dl>552 553<dl class="py method">554<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.end_headers">555<span class="sig-name descname"><span class="pre">end_headers</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.end_headers" title="Link to this definition">¶</a></dt>556<dd><p>Adds a blank line557(indicating the end of the HTTP headers in the response)558to the headers buffer and calls <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.flush_headers" title="http.server.BaseHTTPRequestHandler.flush_headers"><code class="xref py py-meth docutils literal notranslate"><span class="pre">flush_headers()</span></code></a>.</p>559<div class="versionchanged">560<p><span class="versionmodified changed">Changed in version 3.2: </span>The buffered headers are written to the output stream.</p>561</div>562</dd></dl>563 564<dl class="py method">565<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.flush_headers">566<span class="sig-name descname"><span class="pre">flush_headers</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.flush_headers" title="Link to this definition">¶</a></dt>567<dd><p>Finally send the headers to the output stream and flush the internal568headers buffer.</p>569<div class="versionadded">570<p><span class="versionmodified added">Added in version 3.3.</span></p>571</div>572</dd></dl>573 574<dl class="py method">575<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.log_request">576<span class="sig-name descname"><span class="pre">log_request</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">code</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'-'</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">size</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">'-'</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.log_request" title="Link to this definition">¶</a></dt>577<dd><p>Logs an accepted (successful) request. <em>code</em> should specify the numeric578HTTP code associated with the response. If a size of the response is579available, then it should be passed as the <em>size</em> parameter.</p>580</dd></dl>581 582<dl class="py method">583<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.log_error">584<span class="sig-name descname"><span class="pre">log_error</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.log_error" title="Link to this definition">¶</a></dt>585<dd><p>Logs an error when a request cannot be fulfilled. By default, it passes586the message to <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.log_message" title="http.server.BaseHTTPRequestHandler.log_message"><code class="xref py py-meth docutils literal notranslate"><span class="pre">log_message()</span></code></a>, so it takes the same arguments587(<em>format</em> and additional values).</p>588</dd></dl>589 590<dl class="py method">591<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.log_message">592<span class="sig-name descname"><span class="pre">log_message</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">format</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">...</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.log_message" title="Link to this definition">¶</a></dt>593<dd><p>Logs an arbitrary message to <code class="docutils literal notranslate"><span class="pre">sys.stderr</span></code>. This is typically overridden594to create custom error logging mechanisms. The <em>format</em> argument is a595standard printf-style format string, where the additional arguments to596<code class="xref py py-meth docutils literal notranslate"><span class="pre">log_message()</span></code> are applied as inputs to the formatting. The client597ip address and current date and time are prefixed to every message logged.</p>598</dd></dl>599 600<dl class="py method">601<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.version_string">602<span class="sig-name descname"><span class="pre">version_string</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.version_string" title="Link to this definition">¶</a></dt>603<dd><p>Returns the server software’s version string. This is a combination of the604<a class="reference internal" href="#http.server.BaseHTTPRequestHandler.server_version" title="http.server.BaseHTTPRequestHandler.server_version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">server_version</span></code></a> and <a class="reference internal" href="#http.server.BaseHTTPRequestHandler.sys_version" title="http.server.BaseHTTPRequestHandler.sys_version"><code class="xref py py-attr docutils literal notranslate"><span class="pre">sys_version</span></code></a> attributes.</p>605</dd></dl>606 607<dl class="py method">608<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.date_time_string">609<span class="sig-name descname"><span class="pre">date_time_string</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">timestamp</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.date_time_string" title="Link to this definition">¶</a></dt>610<dd><p>Returns the date and time given by <em>timestamp</em> (which must be <code class="docutils literal notranslate"><span class="pre">None</span></code> or in611the format returned by <a class="reference internal" href="time.html#time.time" title="time.time"><code class="xref py py-func docutils literal notranslate"><span class="pre">time.time()</span></code></a>), formatted for a message612header. If <em>timestamp</em> is omitted, it uses the current date and time.</p>613<p>The result looks like <code class="docutils literal notranslate"><span class="pre">'Sun,</span> <span class="pre">06</span> <span class="pre">Nov</span> <span class="pre">1994</span> <span class="pre">08:49:37</span> <span class="pre">GMT'</span></code>.</p>614</dd></dl>615 616<dl class="py method">617<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.log_date_time_string">618<span class="sig-name descname"><span class="pre">log_date_time_string</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.log_date_time_string" title="Link to this definition">¶</a></dt>619<dd><p>Returns the current date and time, formatted for logging.</p>620</dd></dl>621 622<dl class="py method">623<dt class="sig sig-object py" id="http.server.BaseHTTPRequestHandler.address_string">624<span class="sig-name descname"><span class="pre">address_string</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.server.BaseHTTPRequestHandler.address_string" title="Link to this definition">¶</a></dt>625<dd><p>Returns the client address.</p>626<div class="versionchanged">627<p><span class="versionmodified changed">Changed in version 3.3: </span>Previously, a name lookup was performed. To avoid name resolution628delays, it now always returns the IP address.</p>629</div>630</dd></dl>631 632</dd></dl>633 634<dl class="py class">635<dt class="sig sig-object py" id="http.server.SimpleHTTPRequestHandler">636<em class="property"><span class="k"><span class="pre">class</span></span><span class="w"> </span></em><span class="sig-prename descclassname"><span class="pre">http.server.</span></span><span class="sig-name descname"><span class="pre">SimpleHTTPRequestHandler</span></span><span class="sig-paren">(</span><em class="sig-param"><span class="n"><span class="pre">request</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">client_address</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">server</span></span></em>, <em class="sig-param"><span class="n"><span class="pre">directory</span></span><span class="o"><span class="pre">=</span></span><span class="default_value"><span class="pre">None</span></span></em><span class="sig-paren">)</span><a class="headerlink" href="#http.server.SimpleHTTPRequestHandler" title="Link to this definition">¶</a></dt>637<dd><p>This class serves files from the directory <em>directory</em> and below,638or the current directory if <em>directory</em> is not provided, directly639mapping the directory structure to HTTP requests.</p>640<div class="versionchanged">641<p><span class="versionmodified changed">Changed in version 3.7: </span>Added the <em>directory</em> parameter.</p>642</div>643<div class="versionchanged">644<p><span class="versionmodified changed">Changed in version 3.9: </span>The <em>directory</em> parameter accepts a <a class="reference internal" href="../glossary.html#term-path-like-object"><span class="xref std std-term">path-like object</span></a>.</p>645</div>646<p>A lot of the work, such as parsing the request, is done by the base class647<a class="reference internal" href="#http.server.BaseHTTPRequestHandler" title="http.server.BaseHTTPRequestHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHTTPRequestHandler</span></code></a>. This class implements the <a class="reference internal" href="#http.server.SimpleHTTPRequestHandler.do_GET" title="http.server.SimpleHTTPRequestHandler.do_GET"><code class="xref py py-func docutils literal notranslate"><span class="pre">do_GET()</span></code></a>648and <a class="reference internal" href="#http.server.SimpleHTTPRequestHandler.do_HEAD" title="http.server.SimpleHTTPRequestHandler.do_HEAD"><code class="xref py py-func docutils literal notranslate"><span class="pre">do_HEAD()</span></code></a> functions.</p>649<p>The following are defined as class-level attributes of650<code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleHTTPRequestHandler</span></code>:</p>651<dl class="py attribute">652<dt class="sig sig-object py" id="http.server.SimpleHTTPRequestHandler.server_version">653<span class="sig-name descname"><span class="pre">server_version</span></span><a class="headerlink" href="#http.server.SimpleHTTPRequestHandler.server_version" title="Link to this definition">¶</a></dt>654<dd><p>This will be <code class="docutils literal notranslate"><span class="pre">"SimpleHTTP/"</span> <span class="pre">+</span> <span class="pre">__version__</span></code>, where <code class="docutils literal notranslate"><span class="pre">__version__</span></code> is655defined at the module level.</p>656</dd></dl>657 658<dl class="py attribute">659<dt class="sig sig-object py" id="http.server.SimpleHTTPRequestHandler.extensions_map">660<span class="sig-name descname"><span class="pre">extensions_map</span></span><a class="headerlink" href="#http.server.SimpleHTTPRequestHandler.extensions_map" title="Link to this definition">¶</a></dt>661<dd><p>A dictionary mapping suffixes into MIME types, contains custom overrides662for the default system mappings. The mapping is used case-insensitively,663and so should contain only lower-cased keys.</p>664<div class="versionchanged">665<p><span class="versionmodified changed">Changed in version 3.9: </span>This dictionary is no longer filled with the default system mappings,666but only contains overrides.</p>667</div>668</dd></dl>669 670<p>The <code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleHTTPRequestHandler</span></code> class defines the following methods:</p>671<dl class="py method">672<dt class="sig sig-object py" id="http.server.SimpleHTTPRequestHandler.do_HEAD">673<span class="sig-name descname"><span class="pre">do_HEAD</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.server.SimpleHTTPRequestHandler.do_HEAD" title="Link to this definition">¶</a></dt>674<dd><p>This method serves the <code class="docutils literal notranslate"><span class="pre">'HEAD'</span></code> request type: it sends the headers it675would send for the equivalent <code class="docutils literal notranslate"><span class="pre">GET</span></code> request. See the <a class="reference internal" href="#http.server.SimpleHTTPRequestHandler.do_GET" title="http.server.SimpleHTTPRequestHandler.do_GET"><code class="xref py py-meth docutils literal notranslate"><span class="pre">do_GET()</span></code></a>676method for a more complete explanation of the possible headers.</p>677</dd></dl>678 679<dl class="py method">680<dt class="sig sig-object py" id="http.server.SimpleHTTPRequestHandler.do_GET">681<span class="sig-name descname"><span class="pre">do_GET</span></span><span class="sig-paren">(</span><span class="sig-paren">)</span><a class="headerlink" href="#http.server.SimpleHTTPRequestHandler.do_GET" title="Link to this definition">¶</a></dt>682<dd><p>The request is mapped to a local file by interpreting the request as a683path relative to the current working directory.</p>684<p>If the request was mapped to a directory, the directory is checked for a685file 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, the686file’s contents are returned; otherwise a directory listing is generated687by calling the <code class="xref py py-meth docutils literal notranslate"><span class="pre">list_directory()</span></code> method. This method uses688<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> error689response if the <code class="xref py py-func docutils literal notranslate"><span class="pre">listdir()</span></code> fails.</p>690<p>If the request was mapped to a file, it is opened. Any <a class="reference internal" href="exceptions.html#OSError" title="OSError"><code class="xref py py-exc docutils literal notranslate"><span class="pre">OSError</span></code></a>691exception in opening the requested file is mapped to a <code class="docutils literal notranslate"><span class="pre">404</span></code>,692<code class="docutils literal notranslate"><span class="pre">'File</span> <span class="pre">not</span> <span class="pre">found'</span></code> error. If there was an <code class="docutils literal notranslate"><span class="pre">'If-Modified-Since'</span></code>693header in the request, and the file was not modified after this time,694a <code class="docutils literal notranslate"><span class="pre">304</span></code>, <code class="docutils literal notranslate"><span class="pre">'Not</span> <span class="pre">Modified'</span></code> response is sent. Otherwise, the content695type is guessed by calling the <code class="xref py py-meth docutils literal notranslate"><span class="pre">guess_type()</span></code> method, which in turn696uses the <em>extensions_map</em> variable, and the file contents are returned.</p>697<p>A <code class="docutils literal notranslate"><span class="pre">'Content-type:'</span></code> header with the guessed content type is output,698followed by a <code class="docutils literal notranslate"><span class="pre">'Content-Length:'</span></code> header with the file’s size and a699<code class="docutils literal notranslate"><span class="pre">'Last-Modified:'</span></code> header with the file’s modification time.</p>700<p>Then follows a blank line signifying the end of the headers, and then the701contents of the file are output.</p>702<p>For example usage, see the implementation of the <code class="docutils literal notranslate"><span class="pre">test</span></code> function703in <a class="extlink-source reference external" href="https://github.com/python/cpython/tree/main/Lib/http/server.py">Lib/http/server.py</a>.</p>704<div class="versionchanged">705<p><span class="versionmodified changed">Changed in version 3.7: </span>Support of the <code class="docutils literal notranslate"><span class="pre">'If-Modified-Since'</span></code> header.</p>706</div>707</dd></dl>708 709</dd></dl>710 711<p>The <a class="reference internal" href="#http.server.SimpleHTTPRequestHandler" title="http.server.SimpleHTTPRequestHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleHTTPRequestHandler</span></code></a> class can be used in the following712manner in order to create a very basic webserver serving files relative to713the current directory:</p>714<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="kn">import</span><span class="w"> </span><span class="nn">http.server</span>715<span class="kn">import</span><span class="w"> </span><span class="nn">socketserver</span>716 717<span class="n">PORT</span> <span class="o">=</span> <span class="mi">8000</span>718 719<span class="n">Handler</span> <span class="o">=</span> <span class="n">http</span><span class="o">.</span><span class="n">server</span><span class="o">.</span><span class="n">SimpleHTTPRequestHandler</span>720 721<span class="k">with</span> <span class="n">socketserver</span><span class="o">.</span><span class="n">TCPServer</span><span class="p">((</span><span class="s2">""</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">as</span> <span class="n">httpd</span><span class="p">:</span>722 <span class="nb">print</span><span class="p">(</span><span class="s2">"serving at port"</span><span class="p">,</span> <span class="n">PORT</span><span class="p">)</span>723 <span class="n">httpd</span><span class="o">.</span><span class="n">serve_forever</span><span class="p">()</span>724</pre></div>725</div>726<p><a class="reference internal" href="#http.server.SimpleHTTPRequestHandler" title="http.server.SimpleHTTPRequestHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleHTTPRequestHandler</span></code></a> can also be subclassed to enhance behavior,727such as using different index file names by overriding the class attribute728<code class="xref py py-attr docutils literal notranslate"><span class="pre">index_pages</span></code>.</p>729<section id="command-line-interface">730<span id="http-server-cli"></span><h2>Command-line interface<a class="headerlink" href="#command-line-interface" title="Link to this heading">¶</a></h2>731<p><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.server</span></code> 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>732switch of the interpreter. The following example illustrates how to serve733files relative to the current directory:</p>734<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">http</span><span class="o">.</span><span class="n">server</span> <span class="p">[</span><span class="n">OPTIONS</span><span class="p">]</span> <span class="p">[</span><span class="n">port</span><span class="p">]</span>735</pre></div>736</div>737<p>The following options are accepted:</p>738<dl class="std option">739<dt class="sig sig-object std" id="cmdoption-http.server-arg-port">740<span class="sig-name descname"><span class="pre">port</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-http.server-arg-port" title="Link to this definition">¶</a></dt>741<dd><p>The server listens to port 8000 by default. The default can be overridden742by passing the desired port number as an argument:</p>743<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">http</span><span class="o">.</span><span class="n">server</span> <span class="mi">9000</span>744</pre></div>745</div>746</dd></dl>747 748<dl class="std option">749<dt class="sig sig-object std" id="cmdoption-http.server-b">750<span id="cmdoption-http.server-bind"></span><span class="sig-name descname"><span class="pre">-b</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--bind</span></span><span class="sig-prename descclassname"> <span class="pre"><address></span></span><a class="headerlink" href="#cmdoption-http.server-b" title="Link to this definition">¶</a></dt>751<dd><p>Specifies a specific address to which it should bind. Both IPv4 and IPv6752addresses are supported. By default, the server binds itself to all753interfaces. For example, the following command causes the server to bind754to localhost only:</p>755<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">http</span><span class="o">.</span><span class="n">server</span> <span class="o">--</span><span class="n">bind</span> <span class="mf">127.0.0.1</span>756</pre></div>757</div>758<div class="versionadded">759<p><span class="versionmodified added">Added in version 3.4.</span></p>760</div>761<div class="versionchanged">762<p><span class="versionmodified changed">Changed in version 3.8: </span>Support IPv6 in the <code class="docutils literal notranslate"><span class="pre">--bind</span></code> option.</p>763</div>764</dd></dl>765 766<dl class="std option">767<dt class="sig sig-object std" id="cmdoption-http.server-d">768<span id="cmdoption-http.server-directory"></span><span class="sig-name descname"><span class="pre">-d</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--directory</span></span><span class="sig-prename descclassname"> <span class="pre"><dir></span></span><a class="headerlink" href="#cmdoption-http.server-d" title="Link to this definition">¶</a></dt>769<dd><p>Specifies a directory to which it should serve the files. By default,770the server uses the current directory. For example, the following command771uses a specific directory:</p>772<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">http</span><span class="o">.</span><span class="n">server</span> <span class="o">--</span><span class="n">directory</span> <span class="o">/</span><span class="n">tmp</span><span class="o">/</span>773</pre></div>774</div>775<div class="versionadded">776<p><span class="versionmodified added">Added in version 3.7.</span></p>777</div>778</dd></dl>779 780<dl class="std option">781<dt class="sig sig-object std" id="cmdoption-http.server-p">782<span id="cmdoption-http.server-protocol"></span><span class="sig-name descname"><span class="pre">-p</span></span><span class="sig-prename descclassname"></span><span class="sig-prename descclassname"><span class="pre">,</span> </span><span class="sig-name descname"><span class="pre">--protocol</span></span><span class="sig-prename descclassname"> <span class="pre"><version></span></span><a class="headerlink" href="#cmdoption-http.server-p" title="Link to this definition">¶</a></dt>783<dd><p>Specifies the HTTP version to which the server is conformant. By default,784the server is conformant to HTTP/1.0. For example, the following command785runs an HTTP/1.1 conformant server:</p>786<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">http</span><span class="o">.</span><span class="n">server</span> <span class="o">--</span><span class="n">protocol</span> <span class="n">HTTP</span><span class="o">/</span><span class="mf">1.1</span>787</pre></div>788</div>789<div class="versionadded">790<p><span class="versionmodified added">Added in version 3.11.</span></p>791</div>792</dd></dl>793 794<dl class="std option">795<dt class="sig sig-object std" id="cmdoption-http.server-tls-cert">796<span class="sig-name descname"><span class="pre">--tls-cert</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-http.server-tls-cert" title="Link to this definition">¶</a></dt>797<dd><p>Specifies a TLS certificate chain for HTTPS connections:</p>798<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">http</span><span class="o">.</span><span class="n">server</span> <span class="o">--</span><span class="n">tls</span><span class="o">-</span><span class="n">cert</span> <span class="n">fullchain</span><span class="o">.</span><span class="n">pem</span>799</pre></div>800</div>801<div class="versionadded">802<p><span class="versionmodified added">Added in version 3.14.</span></p>803</div>804</dd></dl>805 806<dl class="std option">807<dt class="sig sig-object std" id="cmdoption-http.server-tls-key">808<span class="sig-name descname"><span class="pre">--tls-key</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-http.server-tls-key" title="Link to this definition">¶</a></dt>809<dd><p>Specifies a private key file for HTTPS connections.</p>810<p>This option requires <code class="docutils literal notranslate"><span class="pre">--tls-cert</span></code> to be specified.</p>811<div class="versionadded">812<p><span class="versionmodified added">Added in version 3.14.</span></p>813</div>814</dd></dl>815 816<dl class="std option">817<dt class="sig sig-object std" id="cmdoption-http.server-tls-password-file">818<span class="sig-name descname"><span class="pre">--tls-password-file</span></span><span class="sig-prename descclassname"></span><a class="headerlink" href="#cmdoption-http.server-tls-password-file" title="Link to this definition">¶</a></dt>819<dd><p>Specifies the password file for password-protected private keys:</p>820<div class="highlight-python3 notranslate"><div class="highlight"><pre><span></span><span class="n">python</span> <span class="o">-</span><span class="n">m</span> <span class="n">http</span><span class="o">.</span><span class="n">server</span> \821 <span class="o">--</span><span class="n">tls</span><span class="o">-</span><span class="n">cert</span> <span class="n">cert</span><span class="o">.</span><span class="n">pem</span> \822 <span class="o">--</span><span class="n">tls</span><span class="o">-</span><span class="n">key</span> <span class="n">key</span><span class="o">.</span><span class="n">pem</span> \823 <span class="o">--</span><span class="n">tls</span><span class="o">-</span><span class="n">password</span><span class="o">-</span><span class="n">file</span> <span class="n">password</span><span class="o">.</span><span class="n">txt</span>824</pre></div>825</div>826<p>This option requires <cite>–tls-cert`</cite> to be specified.</p>827<div class="versionadded">828<p><span class="versionmodified added">Added in version 3.14.</span></p>829</div>830</dd></dl>831 832</section>833<section id="security-considerations">834<span id="http-server-security"></span><h2>Security considerations<a class="headerlink" href="#security-considerations" title="Link to this heading">¶</a></h2>835<p id="index-3"><a class="reference internal" href="#http.server.SimpleHTTPRequestHandler" title="http.server.SimpleHTTPRequestHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">SimpleHTTPRequestHandler</span></code></a> will follow symbolic links when handling836requests, this makes it possible for files outside of the specified directory837to be served.</p>838<p>Earlier versions of Python did not scrub control characters from the839log messages emitted to stderr from <code class="docutils literal notranslate"><span class="pre">python</span> <span class="pre">-m</span> <span class="pre">http.server</span></code> or the840default <a class="reference internal" href="#http.server.BaseHTTPRequestHandler" title="http.server.BaseHTTPRequestHandler"><code class="xref py py-class docutils literal notranslate"><span class="pre">BaseHTTPRequestHandler</span></code></a> <code class="docutils literal notranslate"><span class="pre">.log_message</span></code>841implementation. This could allow remote clients connecting to your842server to send nefarious control codes to your terminal.</p>843<div class="versionchanged">844<p><span class="versionmodified changed">Changed in version 3.12: </span>Control characters are scrubbed in stderr logs.</p>845</div>846</section>847</section>848 849 850 <div class="clearer"></div>851 </div>852 </div>853 </div>854 <div class="sphinxsidebar" role="navigation" aria-label="Main">855 <div class="sphinxsidebarwrapper">856 <div>857 <h3><a href="../contents.html">Table of Contents</a></h3>858 <ul>859<li><a class="reference internal" href="#"><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.server</span></code> — HTTP servers</a><ul>860<li><a class="reference internal" href="#command-line-interface">Command-line interface</a></li>861<li><a class="reference internal" href="#security-considerations">Security considerations</a></li>862</ul>863</li>864</ul>865 866 </div>867 <div>868 <h4>Previous topic</h4>869 <p class="topless"><a href="socketserver.html"870 title="previous chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">socketserver</span></code> — A framework for network servers</a></p>871 </div>872 <div>873 <h4>Next topic</h4>874 <p class="topless"><a href="http.cookies.html"875 title="next chapter"><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.cookies</span></code> — HTTP state management</a></p>876 </div>877 <script>878 document.addEventListener('DOMContentLoaded', () => {879 const title = document.querySelector('meta[property="og:title"]').content;880 const elements = document.querySelectorAll('.improvepage');881 const pageurl = window.location.href.split('?')[0];882 elements.forEach(element => {883 const url = new URL(element.href.split('?')[0].replace("-nojs", ""));884 url.searchParams.set('pagetitle', title);885 url.searchParams.set('pageurl', pageurl);886 url.searchParams.set('pagesource', "library/http.server.rst");887 element.href = url.toString();888 });889 });890 </script>891 <div role="note" aria-label="source link">892 <h3>This page</h3>893 <ul class="this-page-menu">894 <li><a href="../bugs.html">Report a bug</a></li>895 <li><a class="improvepage" href="../improve-page-nojs.html">Improve this page</a></li>896 <li>897 <a href="https://github.com/python/cpython/blob/main/Doc/library/http.server.rst?plain=1"898 rel="nofollow">Show source899 </a>900 </li>901 902 </ul>903 </div>904 </div>905<div id="sidebarbutton" title="Collapse sidebar">906<span>«</span>907</div>908 909 </div>910 <div class="clearer"></div>911 </div> 912 <div class="related" role="navigation" aria-label="Related">913 <h3>Navigation</h3>914 <ul>915 <li class="right" style="margin-right: 10px">916 <a href="../genindex.html" title="General Index"917 >index</a></li>918 <li class="right" >919 <a href="../py-modindex.html" title="Python Module Index"920 >modules</a> |</li>921 <li class="right" >922 <a href="http.cookies.html" title="http.cookies — HTTP state management"923 >next</a> |</li>924 <li class="right" >925 <a href="socketserver.html" title="socketserver — A framework for network servers"926 >previous</a> |</li>927 928 <li><img src="../_static/py.svg" alt="Python logo" style="vertical-align: middle; margin-top: -1px"></li>929 <li><a href="https://www.python.org/">Python</a> »</li>930 <li class="switchers">931 <div class="language_switcher_placeholder"></div>932 <div class="version_switcher_placeholder"></div>933 </li>934 <li>935 936 </li>937 <li id="cpython-language-and-version">938 <a href="../index.html">3.15.0a6 Documentation</a> »939 </li>940 941 <li class="nav-item nav-item-1"><a href="index.html" >The Python Standard Library</a> »</li>942 <li class="nav-item nav-item-2"><a href="internet.html" >Internet Protocols and Support</a> »</li>943 <li class="nav-item nav-item-this"><a href=""><code class="xref py py-mod docutils literal notranslate"><span class="pre">http.server</span></code> — HTTP servers</a></li>944 <li class="right">945 946 947 <div class="inline-search" role="search">948 <form class="inline-search" action="../search.html" method="get">949 <input placeholder="Quick search" aria-label="Quick search" type="search" name="q" id="search-box">950 <input type="submit" value="Go">951 </form>952 </div>953 |954 </li>955 <li class="right">956<label class="theme-selector-label">957 Theme958 <select class="theme-selector" oninput="activateTheme(this.value)">959 <option value="auto" selected>Auto</option>960 <option value="light">Light</option>961 <option value="dark">Dark</option>962 </select>963</label> |</li>964 965 </ul>966 </div> 967 <div class="footer">968 © <a href="../copyright.html">Copyright</a> 2001 Python Software Foundation.969 <br>970 This page is licensed under the Python Software Foundation License Version 2.971 <br>972 Examples, recipes, and other code in the documentation are additionally licensed under the Zero Clause BSD License.973 <br>974 975 See <a href="/license.html">History and License</a> for more information.<br>976 977 978 <br>979 980 The Python Software Foundation is a non-profit corporation.981<a href="https://www.python.org/psf/donations/">Please donate.</a>982<br>983 <br>984 Last updated on Mar 10, 2026 (08:58 UTC).985 986 <a href="/bugs.html">Found a bug</a>?987 988 <br>989 990 Created using <a href="https://www.sphinx-doc.org/">Sphinx</a> 8.2.3.991 </div>992 993 </body>994</html>