blob: 4662b4f9a1443bc3291e17a06640c7ea673f642d [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001:mod:`BaseHTTPServer` --- Basic HTTP server
2===========================================
3
4.. module:: BaseHTTPServer
5 :synopsis: Basic HTTP server (base class for SimpleHTTPServer and CGIHTTPServer).
6
Georg Brandl8de91192008-05-26 15:01:48 +00007.. 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.
11
Georg Brandl8ec7f652007-08-15 14:28:01 +000012
13.. index::
14 pair: WWW; server
15 pair: HTTP; protocol
16 single: URL
17 single: httpd
Georg Brandl8ec7f652007-08-15 14:28:01 +000018 module: SimpleHTTPServer
19 module: CGIHTTPServer
20
21This module defines two classes for implementing HTTP servers (Web servers).
22Usually, this module isn't used directly, but is used as a basis for building
23functioning Web servers. See the :mod:`SimpleHTTPServer` and
24:mod:`CGIHTTPServer` modules.
25
Georg Brandle152a772008-05-24 18:31:28 +000026The first class, :class:`HTTPServer`, is a :class:`SocketServer.TCPServer`
Georg Brandl9af0c562009-04-05 10:24:20 +000027subclass, and therefore implements the :class:`SocketServer.BaseServer`
28interface. It creates and listens at the HTTP socket, dispatching the requests
Georg Brandl8ec7f652007-08-15 14:28:01 +000029to a handler. Code to create and run the server looks like this::
30
31 def run(server_class=BaseHTTPServer.HTTPServer,
32 handler_class=BaseHTTPServer.BaseHTTPRequestHandler):
33 server_address = ('', 8000)
34 httpd = server_class(server_address, handler_class)
35 httpd.serve_forever()
36
37
38.. class:: HTTPServer(server_address, RequestHandlerClass)
39
Benjamin Petersonc7b05922008-04-25 01:29:10 +000040 This class builds on the :class:`TCPServer` class by storing the server
41 address as instance variables named :attr:`server_name` and
42 :attr:`server_port`. The server is accessible by the handler, typically
43 through the handler's :attr:`server` instance variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +000044
45
46.. class:: BaseHTTPRequestHandler(request, client_address, server)
47
48 This class is used to handle the HTTP requests that arrive at the server. By
Benjamin Petersonc7b05922008-04-25 01:29:10 +000049 itself, it cannot respond to any actual HTTP requests; it must be subclassed
50 to handle each request method (e.g. GET or
51 POST). :class:`BaseHTTPRequestHandler` provides a number of class and
52 instance variables, and methods for use by subclasses.
Georg Brandl8ec7f652007-08-15 14:28:01 +000053
Benjamin Petersonc7b05922008-04-25 01:29:10 +000054 The handler will parse the request and the headers, then call a method
55 specific to the request type. The method name is constructed from the
56 request. For example, for the request method ``SPAM``, the :meth:`do_SPAM`
57 method will be called with no arguments. All of the relevant information is
58 stored in instance variables of the handler. Subclasses should not need to
59 override or extend the :meth:`__init__` method.
Georg Brandl8ec7f652007-08-15 14:28:01 +000060
Benjamin Petersonc7b05922008-04-25 01:29:10 +000061 :class:`BaseHTTPRequestHandler` has the following instance variables:
Georg Brandl8ec7f652007-08-15 14:28:01 +000062
63
Benjamin Petersonc7b05922008-04-25 01:29:10 +000064 .. attribute:: client_address
Georg Brandl8ec7f652007-08-15 14:28:01 +000065
Benjamin Petersonc7b05922008-04-25 01:29:10 +000066 Contains a tuple of the form ``(host, port)`` referring to the client's
67 address.
Georg Brandl8ec7f652007-08-15 14:28:01 +000068
69
Georg Brandlcff0b462008-08-30 09:49:36 +000070 .. attribute:: server
71
72 Contains the server instance.
73
74
Benjamin Petersonc7b05922008-04-25 01:29:10 +000075 .. attribute:: command
Georg Brandl8ec7f652007-08-15 14:28:01 +000076
Benjamin Petersonc7b05922008-04-25 01:29:10 +000077 Contains the command (request type). For example, ``'GET'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000078
79
Benjamin Petersonc7b05922008-04-25 01:29:10 +000080 .. attribute:: path
Georg Brandl8ec7f652007-08-15 14:28:01 +000081
Benjamin Petersonc7b05922008-04-25 01:29:10 +000082 Contains the request path.
Georg Brandl8ec7f652007-08-15 14:28:01 +000083
84
Benjamin Petersonc7b05922008-04-25 01:29:10 +000085 .. attribute:: request_version
Georg Brandl8ec7f652007-08-15 14:28:01 +000086
Benjamin Petersonc7b05922008-04-25 01:29:10 +000087 Contains the version string from the request. For example, ``'HTTP/1.0'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000088
89
Benjamin Petersonc7b05922008-04-25 01:29:10 +000090 .. attribute:: headers
Georg Brandl8ec7f652007-08-15 14:28:01 +000091
Benjamin Petersonc7b05922008-04-25 01:29:10 +000092 Holds an instance of the class specified by the :attr:`MessageClass` class
93 variable. This instance parses and manages the headers in the HTTP
94 request.
Georg Brandl8ec7f652007-08-15 14:28:01 +000095
96
Benjamin Petersonc7b05922008-04-25 01:29:10 +000097 .. attribute:: rfile
Georg Brandl8ec7f652007-08-15 14:28:01 +000098
Benjamin Petersonc7b05922008-04-25 01:29:10 +000099 Contains an input stream, positioned at the start of the optional input
100 data.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000101
102
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000103 .. attribute:: wfile
Georg Brandl8ec7f652007-08-15 14:28:01 +0000104
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000105 Contains the output stream for writing a response back to the
106 client. Proper adherence to the HTTP protocol must be used when writing to
107 this stream.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000108
Georg Brandl8ec7f652007-08-15 14:28:01 +0000109
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000110 :class:`BaseHTTPRequestHandler` has the following class variables:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000111
Georg Brandl8ec7f652007-08-15 14:28:01 +0000112
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000113 .. attribute:: server_version
Georg Brandl8ec7f652007-08-15 14:28:01 +0000114
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000115 Specifies the server software version. You may want to override this. The
116 format is multiple whitespace-separated strings, where each string is of
117 the form name[/version]. For example, ``'BaseHTTP/0.2'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000118
Georg Brandl8ec7f652007-08-15 14:28:01 +0000119
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000120 .. attribute:: sys_version
Georg Brandl8ec7f652007-08-15 14:28:01 +0000121
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000122 Contains the Python system version, in a form usable by the
123 :attr:`version_string` method and the :attr:`server_version` class
124 variable. For example, ``'Python/1.4'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000125
Georg Brandl8ec7f652007-08-15 14:28:01 +0000126
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000127 .. attribute:: error_message_format
Georg Brandl8ec7f652007-08-15 14:28:01 +0000128
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000129 Specifies a format string for building an error response to the client. It
130 uses parenthesized, keyed format specifiers, so the format operand must be
131 a dictionary. The *code* key should be an integer, specifying the numeric
132 HTTP error code value. *message* should be a string containing a
133 (detailed) error message of what occurred, and *explain* should be an
134 explanation of the error code number. Default *message* and *explain*
135 values can found in the *responses* class variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000136
Georg Brandl16479232008-02-23 15:02:28 +0000137
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000138 .. attribute:: error_content_type
Georg Brandl16479232008-02-23 15:02:28 +0000139
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000140 Specifies the Content-Type HTTP header of error responses sent to the
141 client. The default value is ``'text/html'``.
Georg Brandl16479232008-02-23 15:02:28 +0000142
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000143 .. versionadded:: 2.6
144 Previously, the content type was always ``'text/html'``.
Georg Brandl16479232008-02-23 15:02:28 +0000145
Georg Brandl8ec7f652007-08-15 14:28:01 +0000146
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000147 .. attribute:: protocol_version
Georg Brandl8ec7f652007-08-15 14:28:01 +0000148
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000149 This specifies the HTTP protocol version used in responses. If set to
150 ``'HTTP/1.1'``, the server will permit HTTP persistent connections;
151 however, your server *must* then include an accurate ``Content-Length``
152 header (using :meth:`send_header`) in all of its responses to clients.
153 For backwards compatibility, the setting defaults to ``'HTTP/1.0'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000154
Georg Brandl8ec7f652007-08-15 14:28:01 +0000155
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000156 .. attribute:: MessageClass
157
158 .. index:: single: Message (in module mimetools)
159
160 Specifies a :class:`rfc822.Message`\ -like class to parse HTTP headers.
161 Typically, this is not overridden, and it defaults to
162 :class:`mimetools.Message`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000163
Georg Brandl8ec7f652007-08-15 14:28:01 +0000164
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000165 .. attribute:: responses
Georg Brandl8ec7f652007-08-15 14:28:01 +0000166
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000167 This variable contains a mapping of error code integers to two-element tuples
168 containing a short and long message. For example, ``{code: (shortmessage,
169 longmessage)}``. The *shortmessage* is usually used as the *message* key in an
170 error response, and *longmessage* as the *explain* key (see the
171 :attr:`error_message_format` class variable).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000172
Georg Brandl8ec7f652007-08-15 14:28:01 +0000173
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000174 A :class:`BaseHTTPRequestHandler` instance has the following methods:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000175
176
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000177 .. method:: handle()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000178
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000179 Calls :meth:`handle_one_request` once (or, if persistent connections are
180 enabled, multiple times) to handle incoming HTTP requests. You should
181 never need to override it; instead, implement appropriate :meth:`do_\*`
182 methods.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000183
184
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000185 .. method:: handle_one_request()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000186
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000187 This method will parse and dispatch the request to the appropriate
188 :meth:`do_\*` method. You should never need to override it.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000189
190
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000191 .. method:: send_error(code[, message])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000192
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000193 Sends and logs a complete error reply to the client. The numeric *code*
194 specifies the HTTP error code, with *message* as optional, more specific text. A
195 complete set of headers is sent, followed by text composed using the
196 :attr:`error_message_format` class variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000197
198
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000199 .. method:: send_response(code[, message])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000200
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000201 Sends a response header and logs the accepted request. The HTTP response
202 line is sent, followed by *Server* and *Date* headers. The values for
203 these two headers are picked up from the :meth:`version_string` and
204 :meth:`date_time_string` methods, respectively.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000205
206
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000207 .. method:: send_header(keyword, value)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000208
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000209 Writes a specific HTTP header to the output stream. *keyword* should
210 specify the header keyword, with *value* specifying its value.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000211
212
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000213 .. method:: end_headers()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000214
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000215 Sends a blank line, indicating the end of the HTTP headers in the
216 response.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000217
218
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000219 .. method:: log_request([code[, size]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000220
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000221 Logs an accepted (successful) request. *code* should specify the numeric
222 HTTP code associated with the response. If a size of the response is
223 available, then it should be passed as the *size* parameter.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000224
225
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000226 .. method:: log_error(...)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000227
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000228 Logs an error when a request cannot be fulfilled. By default, it passes
229 the message to :meth:`log_message`, so it takes the same arguments
230 (*format* and additional values).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000231
232
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000233 .. method:: log_message(format, ...)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000234
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000235 Logs an arbitrary message to ``sys.stderr``. This is typically overridden
236 to create custom error logging mechanisms. The *format* argument is a
237 standard printf-style format string, where the additional arguments to
238 :meth:`log_message` are applied as inputs to the formatting. The client
239 address and current date and time are prefixed to every message logged.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000240
241
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000242 .. method:: version_string()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000243
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000244 Returns the server software's version string. This is a combination of the
245 :attr:`server_version` and :attr:`sys_version` class variables.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000246
247
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000248 .. method:: date_time_string([timestamp])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000249
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000250 Returns the date and time given by *timestamp* (which must be in the
251 format returned by :func:`time.time`), formatted for a message header. If
252 *timestamp* is omitted, it uses the current date and time.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000253
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000254 The result looks like ``'Sun, 06 Nov 1994 08:49:37 GMT'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000255
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000256 .. versionadded:: 2.5
257 The *timestamp* parameter.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000258
259
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000260 .. method:: log_date_time_string()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000261
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000262 Returns the current date and time, formatted for logging.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000263
264
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000265 .. method:: address_string()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000266
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000267 Returns the client address, formatted for logging. A name lookup is
268 performed on the client's IP address.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000269
270
Georg Brandl9af0c562009-04-05 10:24:20 +0000271More examples
272-------------
273
274To create a server that doesn't run forever, but until some condition is
275fulfilled::
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()
288
289
Georg Brandl8ec7f652007-08-15 14:28:01 +0000290.. seealso::
291
292 Module :mod:`CGIHTTPServer`
293 Extended request handler that supports CGI scripts.
294
295 Module :mod:`SimpleHTTPServer`
Georg Brandl9af0c562009-04-05 10:24:20 +0000296 Basic request handler that limits response to files actually under the
297 document root.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000298