blob: 64446f4dd42906840001aad26fed615590e77d41 [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
18
19.. index::
20 module: SimpleHTTPServer
21 module: CGIHTTPServer
22
23This module defines two classes for implementing HTTP servers (Web servers).
24Usually, this module isn't used directly, but is used as a basis for building
25functioning Web servers. See the :mod:`SimpleHTTPServer` and
26:mod:`CGIHTTPServer` modules.
27
Georg Brandle152a772008-05-24 18:31:28 +000028The first class, :class:`HTTPServer`, is a :class:`SocketServer.TCPServer`
Georg Brandl8ec7f652007-08-15 14:28:01 +000029subclass. It creates and listens at the HTTP socket, dispatching the requests
30to a handler. Code to create and run the server looks like this::
31
32 def run(server_class=BaseHTTPServer.HTTPServer,
33 handler_class=BaseHTTPServer.BaseHTTPRequestHandler):
34 server_address = ('', 8000)
35 httpd = server_class(server_address, handler_class)
36 httpd.serve_forever()
37
38
39.. class:: HTTPServer(server_address, RequestHandlerClass)
40
Benjamin Petersonc7b05922008-04-25 01:29:10 +000041 This class builds on the :class:`TCPServer` class by storing the server
42 address as instance variables named :attr:`server_name` and
43 :attr:`server_port`. The server is accessible by the handler, typically
44 through the handler's :attr:`server` instance variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +000045
46
47.. class:: BaseHTTPRequestHandler(request, client_address, server)
48
49 This class is used to handle the HTTP requests that arrive at the server. By
Benjamin Petersonc7b05922008-04-25 01:29:10 +000050 itself, it cannot respond to any actual HTTP requests; it must be subclassed
51 to handle each request method (e.g. GET or
52 POST). :class:`BaseHTTPRequestHandler` provides a number of class and
53 instance variables, and methods for use by subclasses.
Georg Brandl8ec7f652007-08-15 14:28:01 +000054
Benjamin Petersonc7b05922008-04-25 01:29:10 +000055 The handler will parse the request and the headers, then call a method
56 specific to the request type. The method name is constructed from the
57 request. For example, for the request method ``SPAM``, the :meth:`do_SPAM`
58 method will be called with no arguments. All of the relevant information is
59 stored in instance variables of the handler. Subclasses should not need to
60 override or extend the :meth:`__init__` method.
Georg Brandl8ec7f652007-08-15 14:28:01 +000061
Benjamin Petersonc7b05922008-04-25 01:29:10 +000062 :class:`BaseHTTPRequestHandler` has the following instance variables:
Georg Brandl8ec7f652007-08-15 14:28:01 +000063
64
Benjamin Petersonc7b05922008-04-25 01:29:10 +000065 .. attribute:: client_address
Georg Brandl8ec7f652007-08-15 14:28:01 +000066
Benjamin Petersonc7b05922008-04-25 01:29:10 +000067 Contains a tuple of the form ``(host, port)`` referring to the client's
68 address.
Georg Brandl8ec7f652007-08-15 14:28:01 +000069
70
Georg Brandlcff0b462008-08-30 09:49:36 +000071 .. attribute:: server
72
73 Contains the server instance.
74
75
Benjamin Petersonc7b05922008-04-25 01:29:10 +000076 .. attribute:: command
Georg Brandl8ec7f652007-08-15 14:28:01 +000077
Benjamin Petersonc7b05922008-04-25 01:29:10 +000078 Contains the command (request type). For example, ``'GET'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000079
80
Benjamin Petersonc7b05922008-04-25 01:29:10 +000081 .. attribute:: path
Georg Brandl8ec7f652007-08-15 14:28:01 +000082
Benjamin Petersonc7b05922008-04-25 01:29:10 +000083 Contains the request path.
Georg Brandl8ec7f652007-08-15 14:28:01 +000084
85
Benjamin Petersonc7b05922008-04-25 01:29:10 +000086 .. attribute:: request_version
Georg Brandl8ec7f652007-08-15 14:28:01 +000087
Benjamin Petersonc7b05922008-04-25 01:29:10 +000088 Contains the version string from the request. For example, ``'HTTP/1.0'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000089
90
Benjamin Petersonc7b05922008-04-25 01:29:10 +000091 .. attribute:: headers
Georg Brandl8ec7f652007-08-15 14:28:01 +000092
Benjamin Petersonc7b05922008-04-25 01:29:10 +000093 Holds an instance of the class specified by the :attr:`MessageClass` class
94 variable. This instance parses and manages the headers in the HTTP
95 request.
Georg Brandl8ec7f652007-08-15 14:28:01 +000096
97
Benjamin Petersonc7b05922008-04-25 01:29:10 +000098 .. attribute:: rfile
Georg Brandl8ec7f652007-08-15 14:28:01 +000099
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000100 Contains an input stream, positioned at the start of the optional input
101 data.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000102
103
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000104 .. attribute:: wfile
Georg Brandl8ec7f652007-08-15 14:28:01 +0000105
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000106 Contains the output stream for writing a response back to the
107 client. Proper adherence to the HTTP protocol must be used when writing to
108 this stream.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000109
Georg Brandl8ec7f652007-08-15 14:28:01 +0000110
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000111 :class:`BaseHTTPRequestHandler` has the following class variables:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000112
Georg Brandl8ec7f652007-08-15 14:28:01 +0000113
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000114 .. attribute:: server_version
Georg Brandl8ec7f652007-08-15 14:28:01 +0000115
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000116 Specifies the server software version. You may want to override this. The
117 format is multiple whitespace-separated strings, where each string is of
118 the form name[/version]. For example, ``'BaseHTTP/0.2'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000119
Georg Brandl8ec7f652007-08-15 14:28:01 +0000120
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000121 .. attribute:: sys_version
Georg Brandl8ec7f652007-08-15 14:28:01 +0000122
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000123 Contains the Python system version, in a form usable by the
124 :attr:`version_string` method and the :attr:`server_version` class
125 variable. For example, ``'Python/1.4'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000126
Georg Brandl8ec7f652007-08-15 14:28:01 +0000127
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000128 .. attribute:: error_message_format
Georg Brandl8ec7f652007-08-15 14:28:01 +0000129
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000130 Specifies a format string for building an error response to the client. It
131 uses parenthesized, keyed format specifiers, so the format operand must be
132 a dictionary. The *code* key should be an integer, specifying the numeric
133 HTTP error code value. *message* should be a string containing a
134 (detailed) error message of what occurred, and *explain* should be an
135 explanation of the error code number. Default *message* and *explain*
136 values can found in the *responses* class variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000137
Georg Brandl16479232008-02-23 15:02:28 +0000138
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000139 .. attribute:: error_content_type
Georg Brandl16479232008-02-23 15:02:28 +0000140
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000141 Specifies the Content-Type HTTP header of error responses sent to the
142 client. The default value is ``'text/html'``.
Georg Brandl16479232008-02-23 15:02:28 +0000143
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000144 .. versionadded:: 2.6
145 Previously, the content type was always ``'text/html'``.
Georg Brandl16479232008-02-23 15:02:28 +0000146
Georg Brandl8ec7f652007-08-15 14:28:01 +0000147
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000148 .. attribute:: protocol_version
Georg Brandl8ec7f652007-08-15 14:28:01 +0000149
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000150 This specifies the HTTP protocol version used in responses. If set to
151 ``'HTTP/1.1'``, the server will permit HTTP persistent connections;
152 however, your server *must* then include an accurate ``Content-Length``
153 header (using :meth:`send_header`) in all of its responses to clients.
154 For backwards compatibility, the setting defaults to ``'HTTP/1.0'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000155
Georg Brandl8ec7f652007-08-15 14:28:01 +0000156
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000157 .. attribute:: MessageClass
158
159 .. index:: single: Message (in module mimetools)
160
161 Specifies a :class:`rfc822.Message`\ -like class to parse HTTP headers.
162 Typically, this is not overridden, and it defaults to
163 :class:`mimetools.Message`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000164
Georg Brandl8ec7f652007-08-15 14:28:01 +0000165
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000166 .. attribute:: responses
Georg Brandl8ec7f652007-08-15 14:28:01 +0000167
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000168 This variable contains a mapping of error code integers to two-element tuples
169 containing a short and long message. For example, ``{code: (shortmessage,
170 longmessage)}``. The *shortmessage* is usually used as the *message* key in an
171 error response, and *longmessage* as the *explain* key (see the
172 :attr:`error_message_format` class variable).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000173
Georg Brandl8ec7f652007-08-15 14:28:01 +0000174
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000175 A :class:`BaseHTTPRequestHandler` instance has the following methods:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000176
177
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000178 .. method:: handle()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000179
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000180 Calls :meth:`handle_one_request` once (or, if persistent connections are
181 enabled, multiple times) to handle incoming HTTP requests. You should
182 never need to override it; instead, implement appropriate :meth:`do_\*`
183 methods.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000184
185
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000186 .. method:: handle_one_request()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000187
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000188 This method will parse and dispatch the request to the appropriate
189 :meth:`do_\*` method. You should never need to override it.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000190
191
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000192 .. method:: send_error(code[, message])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000193
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000194 Sends and logs a complete error reply to the client. The numeric *code*
195 specifies the HTTP error code, with *message* as optional, more specific text. A
196 complete set of headers is sent, followed by text composed using the
197 :attr:`error_message_format` class variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000198
199
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000200 .. method:: send_response(code[, message])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000201
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000202 Sends a response header and logs the accepted request. The HTTP response
203 line is sent, followed by *Server* and *Date* headers. The values for
204 these two headers are picked up from the :meth:`version_string` and
205 :meth:`date_time_string` methods, respectively.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000206
207
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000208 .. method:: send_header(keyword, value)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000209
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000210 Writes a specific HTTP header to the output stream. *keyword* should
211 specify the header keyword, with *value* specifying its value.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000212
213
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000214 .. method:: end_headers()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000215
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000216 Sends a blank line, indicating the end of the HTTP headers in the
217 response.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000218
219
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000220 .. method:: log_request([code[, size]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000221
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000222 Logs an accepted (successful) request. *code* should specify the numeric
223 HTTP code associated with the response. If a size of the response is
224 available, then it should be passed as the *size* parameter.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000225
226
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000227 .. method:: log_error(...)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000228
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000229 Logs an error when a request cannot be fulfilled. By default, it passes
230 the message to :meth:`log_message`, so it takes the same arguments
231 (*format* and additional values).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000232
233
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000234 .. method:: log_message(format, ...)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000235
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000236 Logs an arbitrary message to ``sys.stderr``. This is typically overridden
237 to create custom error logging mechanisms. The *format* argument is a
238 standard printf-style format string, where the additional arguments to
239 :meth:`log_message` are applied as inputs to the formatting. The client
240 address and current date and time are prefixed to every message logged.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000241
242
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000243 .. method:: version_string()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000244
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000245 Returns the server software's version string. This is a combination of the
246 :attr:`server_version` and :attr:`sys_version` class variables.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000247
248
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000249 .. method:: date_time_string([timestamp])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000250
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000251 Returns the date and time given by *timestamp* (which must be in the
252 format returned by :func:`time.time`), formatted for a message header. If
253 *timestamp* is omitted, it uses the current date and time.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000254
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000255 The result looks like ``'Sun, 06 Nov 1994 08:49:37 GMT'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000256
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000257 .. versionadded:: 2.5
258 The *timestamp* parameter.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000259
260
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000261 .. method:: log_date_time_string()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000262
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000263 Returns the current date and time, formatted for logging.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000264
265
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000266 .. method:: address_string()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000267
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000268 Returns the client address, formatted for logging. A name lookup is
269 performed on the client's IP address.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000270
271
272.. seealso::
273
274 Module :mod:`CGIHTTPServer`
275 Extended request handler that supports CGI scripts.
276
277 Module :mod:`SimpleHTTPServer`
278 Basic request handler that limits response to files actually under the document
279 root.
280