⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 module-basehttpserver.html

📁 一本很好的python的说明书,适合对python感兴趣的人
💻 HTML
📖 第 1 页 / 共 2 页
字号:
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<html>
<head>
<title>11.14 BaseHTTPServer -- Basic HTTP server</title>
<META NAME="description" CONTENT="11.14 BaseHTTPServer -- Basic HTTP server">
<META NAME="keywords" CONTENT="lib">
<META NAME="resource-type" CONTENT="document">
<META NAME="distribution" CONTENT="global">
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<link rel="STYLESHEET" href="lib.css" tppabs="http://www.python.org/doc/current/lib/lib.css">
<LINK REL="next" href="module-SimpleHTTPServer.html" tppabs="http://www.python.org/doc/current/lib/module-SimpleHTTPServer.html">
<LINK REL="previous" href="module-SocketServer.html" tppabs="http://www.python.org/doc/current/lib/module-SocketServer.html">
<LINK REL="up" href="internet.html" tppabs="http://www.python.org/doc/current/lib/internet.html">
<LINK REL="next" href="module-SimpleHTTPServer.html" tppabs="http://www.python.org/doc/current/lib/module-SimpleHTTPServer.html">
</head>
<body>
<DIV CLASS="navigation"><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>
<br><hr></DIV>
<!--End of Navigation Panel-->

<H1><A NAME="SECTION00131400000000000000000">
11.14 <tt class="module">BaseHTTPServer</tt> --
         Basic HTTP server</A>
</H1>

<P>


<P>


<P>
This module defines two classes for implementing HTTP servers
(web servers). Usually, this module isn't used directly, but is used
as a basis for building functioning web servers. See the
<tt class="module">SimpleHTTPServer</tt> and
<tt class='module'><a href="module-CGIHTTPServer.html" tppabs="http://www.python.org/doc/current/lib/module-CGIHTTPServer.html">CGIHTTPServer</a></tt> modules.

<P>
The first class, <tt class="class">HTTPServer</tt>, is a
<tt class="class">SocketServer.TCPServer</tt> subclass. It creates and listens at the
web socket, dispatching the requests to a handler. Code to create and
run the server looks like this:

<P>
<dl><dd><pre class="verbatim">
def run(server_class=BaseHTTPServer.HTTPServer,
        handler_class=BaseHTTPServer.BaseHTTPRequestHandler):
    server_address = ('', 8000)
    httpd = server_class(server_address, handler_class)
    httpd.serve_forever()
</pre></dl>

<P>
<dl><dt><b><a name='l2h-2226'><tt class='class'>HTTPServer</tt></a></b> (<var>server_address, RequestHandlerClass</var>)
<dd>
This class builds on the <tt class="class">TCPServer</tt> class by
storing the server address as instance
variables named <tt class="member">server_name</tt> and <tt class="member">server_port</tt>. The
server is accessible by the handler, typically through the handler's
<tt class="member">server</tt> instance variable.
</dl>

<P>
<dl><dt><b><a name='l2h-2227'><tt class='class'>BaseHTTPRequestHandler</tt></a></b> (<var>request, client_address, server</var>)
<dd>
This class is used
to handle the HTTP requests that arrive at the server. By itself,
it cannot respond to any actual HTTP requests; it must be subclassed
to handle each request method (e.g. GET or POST).
<tt class="class">BaseHTTPRequestHandler</tt> provides a number of class and instance
variables, and methods for use by subclasses.

<P>
The handler will parse the request and the headers, then call a
method specific to the request type. The method name is constructed
from the request. For example, for the request method "<tt class="samp">SPAM</tt>", the
<tt class="method">do_SPAM()</tt> method will be called with no arguments. All of
the relevant information is stored in instance variables of the
handler.  Subclasses should not need to override or extend the
<tt class="method">__init__()</tt> method.
</dl>

<P>
<tt class="class">BaseHTTPRequestHandler</tt> has the following instance variables:

<P>
<dl><dt><b><a name='l2h-2228'><tt class='member'>client_address</tt></a></b>
<dd>
Contains a tuple of the form <code>(<var>host</var>, <var>port</var>)</code> referring
to the client's address.
</dl>

<P>
<dl><dt><b><a name='l2h-2229'><tt class='member'>command</tt></a></b>
<dd>
Contains the command (request type). For example, <code>'GET'</code>.
</dl>

<P>
<dl><dt><b><a name='l2h-2230'><tt class='member'>path</tt></a></b>
<dd>
Contains the request path.
</dl>

<P>
<dl><dt><b><a name='l2h-2231'><tt class='member'>request_version</tt></a></b>
<dd>
Contains the version string from the request. For example,
<code>'HTTP/1.0'</code>.
</dl>

<P>
<dl><dt><b><a name='l2h-2232'><tt class='member'>headers</tt></a></b>
<dd>
Holds an instance of the class specified by the <tt class="member">MessageClass</tt>
class variable. This instance parses and manages the headers in
the HTTP request.
</dl>

<P>
<dl><dt><b><a name='l2h-2233'><tt class='member'>rfile</tt></a></b>
<dd>
Contains an input stream, positioned at the start of the optional
input data.
</dl>

<P>
<dl><dt><b><a name='l2h-2234'><tt class='member'>wfile</tt></a></b>
<dd>
Contains the output stream for writing a response back to the client.
Proper adherence to the HTTP protocol must be used when writing
to this stream.
</dl>

<P>
<tt class="class">BaseHTTPRequestHandler</tt> has the following class variables:

<P>
<dl><dt><b><a name='l2h-2235'><tt class='member'>server_version</tt></a></b>
<dd>
Specifies the server software version.  You may want to override
this.
The format is multiple whitespace-separated strings,
where each string is of the form name[/version].
For example, <code>'BaseHTTP/0.2'</code>.
</dl>

<P>
<dl><dt><b><a name='l2h-2236'><tt class='member'>sys_version</tt></a></b>
<dd>
Contains the Python system version, in a form usable by the
<tt class="member">version_string</tt> method and the <tt class="member">server_version</tt> class
variable. For example, <code>'Python/1.4'</code>.
</dl>

<P>
<dl><dt><b><a name='l2h-2237'><tt class='member'>error_message_format</tt></a></b>
<dd>
Specifies a format string for building an error response to the
client. It uses parenthesized, keyed format specifiers, so the
format operand must be a dictionary. The <var>code</var> key should

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -