📄 module-basehttpserver.html
字号:
be an integer, specifying the numeric HTTP error code value.
<var>message</var> should be a string containing a (detailed) error
message of what occurred, and <var>explain</var> should be an
explanation of the error code number. Default <var>message</var>
and <var>explain</var> values can found in the <var>responses</var>
class variable.
</dl>
<P>
<dl><dt><b><a name='l2h-2238'><tt class='member'>protocol_version</tt></a></b>
<dd>
This specifies the HTTP protocol version used in responses.
Typically, this should not be overridden. Defaults to
<code>'HTTP/1.0'</code>.
</dl>
<P>
<dl><dt><b><a name='l2h-2239'><tt class='member'>MessageClass</tt></a></b>
<dd>
Specifies a <tt class="class">rfc822.Message</tt>-like class to parse HTTP
headers. Typically, this is not overridden, and it defaults to
<tt class="class">mimetools.Message</tt>.
</dl>
<P>
<dl><dt><b><a name='l2h-2242'><tt class='member'>responses</tt></a></b>
<dd>
This variable contains a mapping of error code integers to two-element
tuples containing a short and long message. For example,
<code>{<var>code</var>: (<var>shortmessage</var>, <var>longmessage</var>)}</code>. The
<var>shortmessage</var> is usually used as the <var>message</var> key in an
error response, and <var>longmessage</var> as the <var>explain</var> key
(see the <tt class="member">error_message_format</tt> class variable).
</dl>
<P>
A <tt class="class">BaseHTTPRequestHandler</tt> instance has the following methods:
<P>
<dl><dt><b><a name='l2h-2243'><tt class='method'>handle</tt></a></b> ()
<dd>
Overrides the superclass' <tt class="method">handle()</tt> method to provide the
specific handler behavior. This method will parse and dispatch
the request to the appropriate <tt class="method">do_*()</tt> method.
</dl>
<P>
<dl><dt><b><a name='l2h-2244'><tt class='method'>send_error</tt></a></b> (<var>code</var><big>[</big><var>, message</var><big>]</big>)
<dd>
Sends and logs a complete error reply to the client. The numeric
<var>code</var> specifies the HTTP error code, with <var>message</var> as
optional, more specific text. A complete set of headers is sent,
followed by text composed using the <tt class="member">error_message_format</tt>
class variable.
</dl>
<P>
<dl><dt><b><a name='l2h-2245'><tt class='method'>send_response</tt></a></b> (<var>code</var><big>[</big><var>, message</var><big>]</big>)
<dd>
Sends a response header and logs the accepted request. The HTTP
response line is sent, followed by <i>Server</i> and <i>Date</i>
headers. The values for these two headers are picked up from the
<tt class="method">version_string()</tt> and <tt class="method">date_time_string()</tt> methods,
respectively.
</dl>
<P>
<dl><dt><b><a name='l2h-2246'><tt class='method'>send_header</tt></a></b> (<var>keyword, value</var>)
<dd>
Writes a specific MIME header to the output stream. <var>keyword</var>
should specify the header keyword, with <var>value</var> specifying
its value.
</dl>
<P>
<dl><dt><b><a name='l2h-2247'><tt class='method'>end_headers</tt></a></b> ()
<dd>
Sends a blank line, indicating the end of the MIME headers in
the response.
</dl>
<P>
<dl><dt><b><a name='l2h-2248'><tt class='method'>log_request</tt></a></b> (<big>[</big><var>code</var><big>[</big><var>, size</var><big>]</big><big>]</big>)
<dd>
Logs an accepted (successful) request. <var>code</var> should specify
the numeric HTTP code associated with the response. If a size of
the response is available, then it should be passed as the
<var>size</var> parameter.
</dl>
<P>
<dl><dt><b><a name='l2h-2249'><tt class='method'>log_error</tt></a></b> (<var>...</var>)
<dd>
Logs an error when a request cannot be fulfilled. By default,
it passes the message to <tt class="method">log_message()</tt>, so it takes the
same arguments (<var>format</var> and additional values).
</dl>
<P>
<dl><dt><b><a name='l2h-2250'><tt class='method'>log_message</tt></a></b> (<var>format, ...</var>)
<dd>
Logs an arbitrary message to <code>sys.stderr</code>. This is typically
overridden to create custom error logging mechanisms. The
<var>format</var> argument is a standard printf-style format string,
where the additional arguments to <tt class="method">log_message()</tt> are applied
as inputs to the formatting. The client address and current date
and time are prefixed to every message logged.
</dl>
<P>
<dl><dt><b><a name='l2h-2251'><tt class='method'>version_string</tt></a></b> ()
<dd>
Returns the server software's version string. This is a combination
of the <tt class="member">server_version</tt> and <tt class="member">sys_version</tt> class variables.
</dl>
<P>
<dl><dt><b><a name='l2h-2252'><tt class='method'>date_time_string</tt></a></b> ()
<dd>
Returns the current date and time, formatted for a message header.
</dl>
<P>
<dl><dt><b><a name='l2h-2253'><tt class='method'>log_data_time_string</tt></a></b> ()
<dd>
Returns the current date and time, formatted for logging.
</dl>
<P>
<dl><dt><b><a name='l2h-2254'><tt class='method'>address_string</tt></a></b> ()
<dd>
Returns the client address, formatted for logging. A name lookup
is performed on the client's IP address.
</dl>
<P>
<div class='seealso'>
<p class='heading'><b>See Also:</b></p>
<dl compact class="seemodule">
<dt>Module <b><tt class='module'><a href="module-CGIHTTPServer.html" tppabs="http://www.python.org/doc/current/lib/module-CGIHTTPServer.html">CGIHTTPServer</a></tt>:</b>
<dd>Extended request handler that supports CGI
scripts.
</dl>
<P>
<dl compact class="seemodule">
<dt>Module <b><tt class='module'><a href="module-SimpleHTTPServer.html" tppabs="http://www.python.org/doc/current/lib/module-SimpleHTTPServer.html">SimpleHTTPServer</a></tt>:</b>
<dd>Basic request handler that limits response
to files actually under the document root.
</dl>
</div>
<DIV CLASS="navigation"><p><hr><table align="center" width="100%" cellpadding="0" cellspacing="2">
<tr>
<td><A href="module-SocketServer.html" tppabs="http://www.python.org/doc/current/lib/module-SocketServer.html"><img src="previous.gif" tppabs="http://www.python.org/doc/current/icons/previous.gif" border="0" height="32"
alt="Previous Page" width="32"></A></td>
<td><A href="internet.html" tppabs="http://www.python.org/doc/current/lib/internet.html"><img src="up.gif" tppabs="http://www.python.org/doc/current/icons/up.gif" border="0" height="32"
alt="Up One Level" width="32"></A></td>
<td><A href="module-SimpleHTTPServer.html" tppabs="http://www.python.org/doc/current/lib/module-SimpleHTTPServer.html"><img src="next.gif" tppabs="http://www.python.org/doc/current/icons/next.gif" border="0" height="32"
alt="Next Page" width="32"></A></td>
<td align="center" width="100%">Python Library Reference</td>
<td><A href="contents.html" tppabs="http://www.python.org/doc/current/lib/contents.html"><img src="contents.gif" tppabs="http://www.python.org/doc/current/icons/contents.gif" border="0" height="32"
alt="Contents" width="32"></A></td>
<td><a href="modindex.html" tppabs="http://www.python.org/doc/current/lib/modindex.html" title="Module Index"><img src="modules.gif" tppabs="http://www.python.org/doc/current/icons/modules.gif" border="0" height="32"
alt="Module Index" width="32"></a></td>
<td><A href="genindex.html" tppabs="http://www.python.org/doc/current/lib/genindex.html"><img src="index.gif" tppabs="http://www.python.org/doc/current/icons/index.gif" border="0" height="32"
alt="Index" width="32"></A></td>
</tr></table>
<b class="navlabel">Previous:</b> <a class="sectref" href="module-SocketServer.html" tppabs="http://www.python.org/doc/current/lib/module-SocketServer.html">11.13 SocketServer </A>
<b class="navlabel">Up:</b> <a class="sectref" href="internet.html" tppabs="http://www.python.org/doc/current/lib/internet.html">11. Internet Protocols and</A>
<b class="navlabel">Next:</b> <a class="sectref" href="module-SimpleHTTPServer.html" tppabs="http://www.python.org/doc/current/lib/module-SimpleHTTPServer.html">11.15 SimpleHTTPServer </A>
</DIV>
<!--End of Navigation Panel-->
<ADDRESS>
<hr>See <i><a href="about.html" tppabs="http://www.python.org/doc/current/lib/about.html">About this document...</a></i> for information on suggesting changes.
</ADDRESS>
</BODY>
</HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -