blob: 0fbf8b0687dfab2ad8fa5f98e359171303891548 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +00001
2:mod:`BaseHTTPServer` --- Basic HTTP server
3===========================================
4
5.. module:: BaseHTTPServer
6 :synopsis: Basic HTTP server (base class for SimpleHTTPServer and CGIHTTPServer).
7
8
9.. index::
10 pair: WWW; server
11 pair: HTTP; protocol
12 single: URL
13 single: httpd
14
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`
25subclass. 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
Benjamin Petersonc7b05922008-04-25 01:29:10 +000037 This class builds on the :class:`TCPServer` class by storing the server
38 address as instance variables named :attr:`server_name` and
39 :attr:`server_port`. The server is accessible by the handler, typically
40 through the handler's :attr:`server` instance variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +000041
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
Benjamin Petersonc7b05922008-04-25 01:29:10 +000046 itself, it cannot respond to any actual HTTP requests; it must be subclassed
47 to handle each request method (e.g. GET or
48 POST). :class:`BaseHTTPRequestHandler` provides a number of class and
49 instance variables, and methods for use by subclasses.
Georg Brandl8ec7f652007-08-15 14:28:01 +000050
Benjamin Petersonc7b05922008-04-25 01:29:10 +000051 The handler will parse the request and the headers, then call a method
52 specific to the request type. The method name is constructed from the
53 request. For example, for the request method ``SPAM``, the :meth:`do_SPAM`
54 method will be called with no arguments. All of the relevant information is
55 stored in instance variables of the handler. Subclasses should not need to
56 override or extend the :meth:`__init__` method.
Georg Brandl8ec7f652007-08-15 14:28:01 +000057
Benjamin Petersonc7b05922008-04-25 01:29:10 +000058 :class:`BaseHTTPRequestHandler` has the following instance variables:
Georg Brandl8ec7f652007-08-15 14:28:01 +000059
60
Benjamin Petersonc7b05922008-04-25 01:29:10 +000061 .. attribute:: client_address
Georg Brandl8ec7f652007-08-15 14:28:01 +000062
Benjamin Petersonc7b05922008-04-25 01:29:10 +000063 Contains a tuple of the form ``(host, port)`` referring to the client's
64 address.
Georg Brandl8ec7f652007-08-15 14:28:01 +000065
66
Benjamin Petersonc7b05922008-04-25 01:29:10 +000067 .. attribute:: command
Georg Brandl8ec7f652007-08-15 14:28:01 +000068
Benjamin Petersonc7b05922008-04-25 01:29:10 +000069 Contains the command (request type). For example, ``'GET'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000070
71
Benjamin Petersonc7b05922008-04-25 01:29:10 +000072 .. attribute:: path
Georg Brandl8ec7f652007-08-15 14:28:01 +000073
Benjamin Petersonc7b05922008-04-25 01:29:10 +000074 Contains the request path.
Georg Brandl8ec7f652007-08-15 14:28:01 +000075
76
Benjamin Petersonc7b05922008-04-25 01:29:10 +000077 .. attribute:: request_version
Georg Brandl8ec7f652007-08-15 14:28:01 +000078
Benjamin Petersonc7b05922008-04-25 01:29:10 +000079 Contains the version string from the request. For example, ``'HTTP/1.0'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000080
81
Benjamin Petersonc7b05922008-04-25 01:29:10 +000082 .. attribute:: headers
Georg Brandl8ec7f652007-08-15 14:28:01 +000083
Benjamin Petersonc7b05922008-04-25 01:29:10 +000084 Holds an instance of the class specified by the :attr:`MessageClass` class
85 variable. This instance parses and manages the headers in the HTTP
86 request.
Georg Brandl8ec7f652007-08-15 14:28:01 +000087
88
Benjamin Petersonc7b05922008-04-25 01:29:10 +000089 .. attribute:: rfile
Georg Brandl8ec7f652007-08-15 14:28:01 +000090
Benjamin Petersonc7b05922008-04-25 01:29:10 +000091 Contains an input stream, positioned at the start of the optional input
92 data.
Georg Brandl8ec7f652007-08-15 14:28:01 +000093
94
Benjamin Petersonc7b05922008-04-25 01:29:10 +000095 .. attribute:: wfile
Georg Brandl8ec7f652007-08-15 14:28:01 +000096
Benjamin Petersonc7b05922008-04-25 01:29:10 +000097 Contains the output stream for writing a response back to the
98 client. Proper adherence to the HTTP protocol must be used when writing to
99 this stream.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000100
Georg Brandl8ec7f652007-08-15 14:28:01 +0000101
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000102 :class:`BaseHTTPRequestHandler` has the following class variables:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000103
Georg Brandl8ec7f652007-08-15 14:28:01 +0000104
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000105 .. attribute:: server_version
Georg Brandl8ec7f652007-08-15 14:28:01 +0000106
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000107 Specifies the server software version. You may want to override this. The
108 format is multiple whitespace-separated strings, where each string is of
109 the form name[/version]. For example, ``'BaseHTTP/0.2'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000110
Georg Brandl8ec7f652007-08-15 14:28:01 +0000111
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000112 .. attribute:: sys_version
Georg Brandl8ec7f652007-08-15 14:28:01 +0000113
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000114 Contains the Python system version, in a form usable by the
115 :attr:`version_string` method and the :attr:`server_version` class
116 variable. For example, ``'Python/1.4'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000117
Georg Brandl8ec7f652007-08-15 14:28:01 +0000118
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000119 .. attribute:: error_message_format
Georg Brandl8ec7f652007-08-15 14:28:01 +0000120
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000121 Specifies a format string for building an error response to the client. It
122 uses parenthesized, keyed format specifiers, so the format operand must be
123 a dictionary. The *code* key should be an integer, specifying the numeric
124 HTTP error code value. *message* should be a string containing a
125 (detailed) error message of what occurred, and *explain* should be an
126 explanation of the error code number. Default *message* and *explain*
127 values can found in the *responses* class variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000128
Georg Brandl16479232008-02-23 15:02:28 +0000129
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000130 .. attribute:: error_content_type
Georg Brandl16479232008-02-23 15:02:28 +0000131
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000132 Specifies the Content-Type HTTP header of error responses sent to the
133 client. The default value is ``'text/html'``.
Georg Brandl16479232008-02-23 15:02:28 +0000134
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000135 .. versionadded:: 2.6
136 Previously, the content type was always ``'text/html'``.
Georg Brandl16479232008-02-23 15:02:28 +0000137
Georg Brandl8ec7f652007-08-15 14:28:01 +0000138
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000139 .. attribute:: protocol_version
Georg Brandl8ec7f652007-08-15 14:28:01 +0000140
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000141 This specifies the HTTP protocol version used in responses. If set to
142 ``'HTTP/1.1'``, the server will permit HTTP persistent connections;
143 however, your server *must* then include an accurate ``Content-Length``
144 header (using :meth:`send_header`) in all of its responses to clients.
145 For backwards compatibility, the setting defaults to ``'HTTP/1.0'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000146
Georg Brandl8ec7f652007-08-15 14:28:01 +0000147
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000148 .. attribute:: MessageClass
149
150 .. index:: single: Message (in module mimetools)
151
152 Specifies a :class:`rfc822.Message`\ -like class to parse HTTP headers.
153 Typically, this is not overridden, and it defaults to
154 :class:`mimetools.Message`.
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:: responses
Georg Brandl8ec7f652007-08-15 14:28:01 +0000158
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000159 This variable contains a mapping of error code integers to two-element tuples
160 containing a short and long message. For example, ``{code: (shortmessage,
161 longmessage)}``. The *shortmessage* is usually used as the *message* key in an
162 error response, and *longmessage* as the *explain* key (see the
163 :attr:`error_message_format` class variable).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000164
Georg Brandl8ec7f652007-08-15 14:28:01 +0000165
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000166 A :class:`BaseHTTPRequestHandler` instance has the following methods:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000167
168
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000169 .. method:: handle()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000170
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000171 Calls :meth:`handle_one_request` once (or, if persistent connections are
172 enabled, multiple times) to handle incoming HTTP requests. You should
173 never need to override it; instead, implement appropriate :meth:`do_\*`
174 methods.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000175
176
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000177 .. method:: handle_one_request()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000178
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000179 This method will parse and dispatch the request to the appropriate
180 :meth:`do_\*` method. You should never need to override it.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000181
182
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000183 .. method:: send_error(code[, message])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000184
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000185 Sends and logs a complete error reply to the client. The numeric *code*
186 specifies the HTTP error code, with *message* as optional, more specific text. A
187 complete set of headers is sent, followed by text composed using the
188 :attr:`error_message_format` class variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000189
190
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000191 .. method:: send_response(code[, message])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000192
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000193 Sends a response header and logs the accepted request. The HTTP response
194 line is sent, followed by *Server* and *Date* headers. The values for
195 these two headers are picked up from the :meth:`version_string` and
196 :meth:`date_time_string` methods, respectively.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000197
198
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000199 .. method:: send_header(keyword, value)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000200
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000201 Writes a specific HTTP header to the output stream. *keyword* should
202 specify the header keyword, with *value* specifying its value.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000203
204
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000205 .. method:: end_headers()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000206
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000207 Sends a blank line, indicating the end of the HTTP headers in the
208 response.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000209
210
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000211 .. method:: log_request([code[, size]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000212
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000213 Logs an accepted (successful) request. *code* should specify the numeric
214 HTTP code associated with the response. If a size of the response is
215 available, then it should be passed as the *size* parameter.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000216
217
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000218 .. method:: log_error(...)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000219
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000220 Logs an error when a request cannot be fulfilled. By default, it passes
221 the message to :meth:`log_message`, so it takes the same arguments
222 (*format* and additional values).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000223
224
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000225 .. method:: log_message(format, ...)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000226
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000227 Logs an arbitrary message to ``sys.stderr``. This is typically overridden
228 to create custom error logging mechanisms. The *format* argument is a
229 standard printf-style format string, where the additional arguments to
230 :meth:`log_message` are applied as inputs to the formatting. The client
231 address and current date and time are prefixed to every message logged.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000232
233
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000234 .. method:: version_string()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000235
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000236 Returns the server software's version string. This is a combination of the
237 :attr:`server_version` and :attr:`sys_version` class variables.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000238
239
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000240 .. method:: date_time_string([timestamp])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000241
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000242 Returns the date and time given by *timestamp* (which must be in the
243 format returned by :func:`time.time`), formatted for a message header. If
244 *timestamp* is omitted, it uses the current date and time.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000245
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000246 The result looks like ``'Sun, 06 Nov 1994 08:49:37 GMT'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000247
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000248 .. versionadded:: 2.5
249 The *timestamp* parameter.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000250
251
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000252 .. method:: log_date_time_string()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000253
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000254 Returns the current date and time, formatted for logging.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000255
256
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000257 .. method:: address_string()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000258
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000259 Returns the client address, formatted for logging. A name lookup is
260 performed on the client's IP address.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000261
262
263.. seealso::
264
265 Module :mod:`CGIHTTPServer`
266 Extended request handler that supports CGI scripts.
267
268 Module :mod:`SimpleHTTPServer`
269 Basic request handler that limits response to files actually under the document
270 root.
271