rest25/library/basehttpserver.rst => rest262/library/basehttpserver.rst
n1- 
2:mod:`BaseHTTPServer` --- Basic HTTP server
3===========================================
4
5.. module:: BaseHTTPServer
6   :synopsis: Basic HTTP server (base class for SimpleHTTPServer and CGIHTTPServer).
n6+ 
7+.. note::
8+   The :mod:`BaseHTTPServer` module has been merged into :mod:`http.server` in
9+   Python 3.0.  The :term:`2to3` tool will automatically adapt imports when
10+   converting your sources to 3.0.
7
8
9.. index::
10   pair: WWW; server
11   pair: HTTP; protocol
12   single: URL
13   single: httpd
n14- 
15-.. index::
16   module: SimpleHTTPServer
17   module: CGIHTTPServer
18
19This module defines two classes for implementing HTTP servers (Web servers).
20Usually, this module isn't used directly, but is used as a basis for building
21functioning Web servers. See the :mod:`SimpleHTTPServer` and
22:mod:`CGIHTTPServer` modules.
23
24The first class, :class:`HTTPServer`, is a :class:`SocketServer.TCPServer`
n27+subclass, and therefore implements the :class:`SocketServer.BaseServer`
25-subclass.  It creates and listens at the HTTP socket, dispatching the requests
28+interface.  It creates and listens at the HTTP socket, dispatching the requests
26to a handler.  Code to create and run the server looks like this::
27
28   def run(server_class=BaseHTTPServer.HTTPServer,
29           handler_class=BaseHTTPServer.BaseHTTPRequestHandler):
30       server_address = ('', 8000)
31       httpd = server_class(server_address, handler_class)
32       httpd.serve_forever()
33
34
35.. class:: HTTPServer(server_address, RequestHandlerClass)
36
n37-   This class builds on the :class:`TCPServer` class by storing the server address
n40+   This class builds on the :class:`TCPServer` class by storing the server
38-   as instance variables named :attr:`server_name` and :attr:`server_port`. The
41+   address as instance variables named :attr:`server_name` and
39-   server is accessible by the handler, typically through the handler's
42+   :attr:`server_port`. The server is accessible by the handler, typically
40-   :attr:`server` instance variable.
43+   through the handler's :attr:`server` instance variable.
41
42
43.. class:: BaseHTTPRequestHandler(request, client_address, server)
44
45   This class is used to handle the HTTP requests that arrive at the server. By
n46-   itself, it cannot respond to any actual HTTP requests; it must be subclassed to
n49+   itself, it cannot respond to any actual HTTP requests; it must be subclassed
47-   handle each request method (e.g. GET or POST). :class:`BaseHTTPRequestHandler`
50+   to handle each request method (e.g. GET or
48-   provides a number of class and instance variables, and methods for use by
51+   POST). :class:`BaseHTTPRequestHandler` provides a number of class and
49-   subclasses.
52+   instance variables, and methods for use by subclasses.
50
n51-   The handler will parse the request and the headers, then call a method specific
n54+   The handler will parse the request and the headers, then call a method
52-   to the request type. The method name is constructed from the request. For
55+   specific to the request type. The method name is constructed from the
53-   example, for the request method ``SPAM``, the :meth:`do_SPAM` method will be
56+   request. For example, for the request method ``SPAM``, the :meth:`do_SPAM`
54-   called with no arguments. All of the relevant information is stored in instance
57+   method will be called with no arguments. All of the relevant information is
55-   variables of the handler.  Subclasses should not need to override or extend the
58+   stored in instance variables of the handler.  Subclasses should not need to
56-   :meth:`__init__` method.
59+   override or extend the :meth:`__init__` method.
57
n58-:class:`BaseHTTPRequestHandler` has the following instance variables:
n61+   :class:`BaseHTTPRequestHandler` has the following instance variables:
59
60
n61-.. attribute:: BaseHTTPRequestHandler.client_address
n64+   .. attribute:: client_address
62
n63-   Contains a tuple of the form ``(host, port)`` referring to the client's address.
n66+      Contains a tuple of the form ``(host, port)`` referring to the client's
67+      address.
64
65
n66-.. attribute:: BaseHTTPRequestHandler.command
n70+   .. attribute:: server
67
n72+      Contains the server instance.
73+ 
74+ 
75+   .. attribute:: command
76+ 
68-   Contains the command (request type). For example, ``'GET'``.
77+      Contains the command (request type). For example, ``'GET'``.
69
70
n71-.. attribute:: BaseHTTPRequestHandler.path
n80+   .. attribute:: path
72
n73-   Contains the request path.
n82+      Contains the request path.
74
75
n76-.. attribute:: BaseHTTPRequestHandler.request_version
n85+   .. attribute:: request_version
77
n78-   Contains the version string from the request. For example, ``'HTTP/1.0'``.
n87+      Contains the version string from the request. For example, ``'HTTP/1.0'``.
79
80
n81-.. attribute:: BaseHTTPRequestHandler.headers
n90+   .. attribute:: headers
82
n83-   Holds an instance of the class specified by the :attr:`MessageClass` class
n92+      Holds an instance of the class specified by the :attr:`MessageClass` class
84-   variable. This instance parses and manages the headers in the HTTP request.
93+      variable. This instance parses and manages the headers in the HTTP
94+      request.
85
86
n87-.. attribute:: BaseHTTPRequestHandler.rfile
n97+   .. attribute:: rfile
88
n89-   Contains an input stream, positioned at the start of the optional input data.
n99+      Contains an input stream, positioned at the start of the optional input
100+      data.
90
91
n92-.. attribute:: BaseHTTPRequestHandler.wfile
n103+   .. attribute:: wfile
93
n94-   Contains the output stream for writing a response back to the client. Proper
n105+      Contains the output stream for writing a response back to the
95-   adherence to the HTTP protocol must be used when writing to this stream.
106+      client. Proper adherence to the HTTP protocol must be used when writing to
107+      this stream.
96
n109+ 
97-:class:`BaseHTTPRequestHandler` has the following class variables:
110+   :class:`BaseHTTPRequestHandler` has the following class variables:
98
99
n100-.. attribute:: BaseHTTPRequestHandler.server_version
n113+   .. attribute:: server_version
101
n102-   Specifies the server software version.  You may want to override this. The
n115+      Specifies the server software version.  You may want to override this. The
103-   format is multiple whitespace-separated strings, where each string is of the
116+      format is multiple whitespace-separated strings, where each string is of
104-   form name[/version]. For example, ``'BaseHTTP/0.2'``.
117+      the form name[/version]. For example, ``'BaseHTTP/0.2'``.
105
106
n107-.. attribute:: BaseHTTPRequestHandler.sys_version
n120+   .. attribute:: sys_version
108
n109-   Contains the Python system version, in a form usable by the
n122+      Contains the Python system version, in a form usable by the
110-   :attr:`version_string` method and the :attr:`server_version` class variable. For
123+      :attr:`version_string` method and the :attr:`server_version` class
111-   example, ``'Python/1.4'``.
124+      variable. For example, ``'Python/1.4'``.
112
113
n114-.. attribute:: BaseHTTPRequestHandler.error_message_format
n127+   .. attribute:: error_message_format
115
n116-   Specifies a format string for building an error response to the client. It uses
n129+      Specifies a format string for building an error response to the client. It
117-   parenthesized, keyed format specifiers, so the format operand must be a
130+      uses parenthesized, keyed format specifiers, so the format operand must be
118-   dictionary. The *code* key should be an integer, specifying the numeric HTTP
131+      a dictionary. The *code* key should be an integer, specifying the numeric
119-   error code value. *message* should be a string containing a (detailed) error
132+      HTTP error code value. *message* should be a string containing a
120-   message of what occurred, and *explain* should be an explanation of the error
133+      (detailed) error message of what occurred, and *explain* should be an
121-   code number. Default *message* and *explain* values can found in the *responses*
134+      explanation of the error code number. Default *message* and *explain*
122-   class variable.
135+      values can found in the *responses* class variable.
123
124
n125-.. attribute:: BaseHTTPRequestHandler.protocol_version
n138+   .. attribute:: error_content_type
126
n140+      Specifies the Content-Type HTTP header of error responses sent to the
141+      client.  The default value is ``'text/html'``.
142+ 
143+      .. versionadded:: 2.6
144+         Previously, the content type was always ``'text/html'``.
145+ 
146+ 
147+   .. attribute:: protocol_version
148+ 
127-   This specifies the HTTP protocol version used in responses.  If set to
149+      This specifies the HTTP protocol version used in responses.  If set to
128-   ``'HTTP/1.1'``, the server will permit HTTP persistent connections; however,
150+      ``'HTTP/1.1'``, the server will permit HTTP persistent connections;
129-   your server *must* then include an accurate ``Content-Length`` header (using
151+      however, your server *must* then include an accurate ``Content-Length``
130-   :meth:`send_header`) in all of its responses to clients.  For backwards
152+      header (using :meth:`send_header`) in all of its responses to clients.
131-   compatibility, the setting defaults to ``'HTTP/1.0'``.
153+      For backwards compatibility, the setting defaults to ``'HTTP/1.0'``.
132
133
n134-.. attribute:: BaseHTTPRequestHandler.MessageClass
n156+   .. attribute:: MessageClass
135
n136-   .. index:: single: Message (in module mimetools)
n158+      .. index:: single: Message (in module mimetools)
137
n138-   Specifies a :class:`rfc822.Message`\ -like class to parse HTTP headers.
n160+      Specifies a :class:`rfc822.Message`\ -like class to parse HTTP headers.
139-   Typically, this is not overridden, and it defaults to
161+      Typically, this is not overridden, and it defaults to
140-   :class:`mimetools.Message`.
162+      :class:`mimetools.Message`.
141
142
n143-.. attribute:: BaseHTTPRequestHandler.responses
n165+   .. attribute:: responses
144
n145-   This variable contains a mapping of error code integers to two-element tuples
n167+      This variable contains a mapping of error code integers to two-element tuples
146-   containing a short and long message. For example, ``{code: (shortmessage,
168+      containing a short and long message. For example, ``{code: (shortmessage,
147-   longmessage)}``. The *shortmessage* is usually used as the *message* key in an
169+      longmessage)}``. The *shortmessage* is usually used as the *message* key in an
148-   error response, and *longmessage* as the *explain* key (see the
170+      error response, and *longmessage* as the *explain* key (see the
149-   :attr:`error_message_format` class variable).
171+      :attr:`error_message_format` class variable).
150
n173+ 
151-A :class:`BaseHTTPRequestHandler` instance has the following methods:
174+   A :class:`BaseHTTPRequestHandler` instance has the following methods:
152
153
n154-.. method:: BaseHTTPRequestHandler.handle()
n177+   .. method:: handle()
155
n156-   Calls :meth:`handle_one_request` once (or, if persistent connections are
n179+      Calls :meth:`handle_one_request` once (or, if persistent connections are
157-   enabled, multiple times) to handle incoming HTTP requests. You should never need
180+      enabled, multiple times) to handle incoming HTTP requests. You should
158-   to override it; instead, implement appropriate :meth:`do_\*` methods.
181+      never need to override it; instead, implement appropriate :meth:`do_\*`
182+      methods.
159
160
n161-.. method:: BaseHTTPRequestHandler.handle_one_request()
n185+   .. method:: handle_one_request()
162
n163-   This method will parse and dispatch the request to the appropriate :meth:`do_\*`
n187+      This method will parse and dispatch the request to the appropriate
164-   method.  You should never need to override it.
188+      :meth:`do_\*` method.  You should never need to override it.
165
166
n167-.. method:: BaseHTTPRequestHandler.send_error(code[, message])
n191+   .. method:: send_error(code[, message])
168
n169-   Sends and logs a complete error reply to the client. The numeric *code*
n193+      Sends and logs a complete error reply to the client. The numeric *code*
170-   specifies the HTTP error code, with *message* as optional, more specific text. A
194+      specifies the HTTP error code, with *message* as optional, more specific text. A
171-   complete set of headers is sent, followed by text composed using the
195+      complete set of headers is sent, followed by text composed using the
172-   :attr:`error_message_format` class variable.
196+      :attr:`error_message_format` class variable.
173
174
n175-.. method:: BaseHTTPRequestHandler.send_response(code[, message])
n199+   .. method:: send_response(code[, message])
176
n177-   Sends a response header and logs the accepted request. The HTTP response line is
n201+      Sends a response header and logs the accepted request. The HTTP response
178-   sent, followed by *Server* and *Date* headers. The values for these two headers
202+      line is sent, followed by *Server* and *Date* headers. The values for
179-   are picked up from the :meth:`version_string` and :meth:`date_time_string`
203+      these two headers are picked up from the :meth:`version_string` and
180-   methods, respectively.
204+      :meth:`date_time_string` methods, respectively.
181
182
n183-.. method:: BaseHTTPRequestHandler.send_header(keyword, value)
n207+   .. method:: send_header(keyword, value)
184
n185-   Writes a specific HTTP header to the output stream. *keyword* should specify the
n209+      Writes a specific HTTP header to the output stream. *keyword* should
186-   header keyword, with *value* specifying its value.
210+      specify the header keyword, with *value* specifying its value.
187
188
n189-.. method:: BaseHTTPRequestHandler.end_headers()
n213+   .. method:: end_headers()
190
n191-   Sends a blank line, indicating the end of the HTTP headers in the response.
n215+      Sends a blank line, indicating the end of the HTTP headers in the
216+      response.
192
193
n194-.. method:: BaseHTTPRequestHandler.log_request([code[, size]])
n219+   .. method:: log_request([code[, size]])
195
n196-   Logs an accepted (successful) request. *code* should specify the numeric HTTP
n221+      Logs an accepted (successful) request. *code* should specify the numeric
197-   code associated with the response. If a size of the response is available, then
222+      HTTP code associated with the response. If a size of the response is
198-   it should be passed as the *size* parameter.
223+      available, then it should be passed as the *size* parameter.
199
200
n201-.. method:: BaseHTTPRequestHandler.log_error(...)
n226+   .. method:: log_error(...)
202
n203-   Logs an error when a request cannot be fulfilled. By default, it passes the
n228+      Logs an error when a request cannot be fulfilled. By default, it passes
204-   message to :meth:`log_message`, so it takes the same arguments (*format* and
229+      the message to :meth:`log_message`, so it takes the same arguments
205-   additional values).
230+      (*format* and additional values).
206
207
n208-.. method:: BaseHTTPRequestHandler.log_message(format, ...)
n233+   .. method:: log_message(format, ...)
209
n210-   Logs an arbitrary message to ``sys.stderr``. This is typically overridden to
n235+      Logs an arbitrary message to ``sys.stderr``. This is typically overridden
211-   create custom error logging mechanisms. The *format* argument is a standard
236+      to create custom error logging mechanisms. The *format* argument is a
212-   printf-style format string, where the additional arguments to
237+      standard printf-style format string, where the additional arguments to
213-   :meth:`log_message` are applied as inputs to the formatting. The client address
238+      :meth:`log_message` are applied as inputs to the formatting. The client
214-   and current date and time are prefixed to every message logged.
239+      address and current date and time are prefixed to every message logged.
215
216
n217-.. method:: BaseHTTPRequestHandler.version_string()
n242+   .. method:: version_string()
218
n219-   Returns the server software's version string. This is a combination of the
n244+      Returns the server software's version string. This is a combination of the
220-   :attr:`server_version` and :attr:`sys_version` class variables.
245+      :attr:`server_version` and :attr:`sys_version` class variables.
221
222
n223-.. method:: BaseHTTPRequestHandler.date_time_string([timestamp])
n248+   .. method:: date_time_string([timestamp])
224
n225-   Returns the date and time given by *timestamp* (which must be in the format
n250+      Returns the date and time given by *timestamp* (which must be in the
226-   returned by :func:`time.time`), formatted for a message header. If *timestamp*
251+      format returned by :func:`time.time`), formatted for a message header. If
227-   is omitted, it uses the current date and time.
252+      *timestamp* is omitted, it uses the current date and time.
228
n229-   The result looks like ``'Sun, 06 Nov 1994 08:49:37 GMT'``.
n254+      The result looks like ``'Sun, 06 Nov 1994 08:49:37 GMT'``.
230
n231-   .. versionadded:: 2.5
n256+      .. versionadded:: 2.5
232-      The *timestamp* parameter.
257+         The *timestamp* parameter.
233
234
n235-.. method:: BaseHTTPRequestHandler.log_date_time_string()
n260+   .. method:: log_date_time_string()
236
n237-   Returns the current date and time, formatted for logging.
n262+      Returns the current date and time, formatted for logging.
238
239
n240-.. method:: BaseHTTPRequestHandler.address_string()
n265+   .. method:: address_string()
241
n242-   Returns the client address, formatted for logging. A name lookup is performed on
n267+      Returns the client address, formatted for logging. A name lookup is
243-   the client's IP address.
268+      performed on the client's IP address.
269+ 
270+ 
271+More examples
272+-------------
273+ 
274+To create a server that doesn't run forever, but until some condition is
275+fulfilled::
276+ 
277+   def run_while_true(server_class=BaseHTTPServer.HTTPServer,
278+                      handler_class=BaseHTTPServer.BaseHTTPRequestHandler):
279+       """
280+       This assumes that keep_running() is a function of no arguments which
281+       is tested initially and after each request.  If its return value
282+       is true, the server continues.
283+       """
284+       server_address = ('', 8000)
285+       httpd = server_class(server_address, handler_class)
286+       while keep_running():
287+           httpd.handle_request()
244
245
246.. seealso::
247
248   Module :mod:`CGIHTTPServer`
249      Extended request handler that supports CGI scripts.
250
251   Module :mod:`SimpleHTTPServer`
t252-      Basic request handler that limits response to files actually under the document
t296+      Basic request handler that limits response to files actually under the
253-      root.
297+      document root.
254
Legends
Colors
 Added 
Changed
Deleted
Links
(f)irst change
(n)ext change
(t)op