blob: 52a3866971f624acf4f8cc961171d1a93fd6827e [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +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
Alexandre Vassalottice261952008-05-12 02:31:37 +000024The first class, :class:`HTTPServer`, is a :class:`socketserver.TCPServer`
Georg Brandl116aa622007-08-15 14:28:22 +000025subclass. 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 Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +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 Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +000050
Benjamin Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +000057
Benjamin Petersone41251e2008-04-25 01:59:09 +000058 :class:`BaseHTTPRequestHandler` has the following instance variables:
Georg Brandl116aa622007-08-15 14:28:22 +000059
60
Benjamin Petersone41251e2008-04-25 01:59:09 +000061 .. attribute:: client_address
Georg Brandl116aa622007-08-15 14:28:22 +000062
Benjamin Petersone41251e2008-04-25 01:59:09 +000063 Contains a tuple of the form ``(host, port)`` referring to the client's
64 address.
Georg Brandl116aa622007-08-15 14:28:22 +000065
66
Benjamin Petersone41251e2008-04-25 01:59:09 +000067 .. attribute:: command
Georg Brandl116aa622007-08-15 14:28:22 +000068
Benjamin Petersone41251e2008-04-25 01:59:09 +000069 Contains the command (request type). For example, ``'GET'``.
Georg Brandl116aa622007-08-15 14:28:22 +000070
71
Benjamin Petersone41251e2008-04-25 01:59:09 +000072 .. attribute:: path
Georg Brandl116aa622007-08-15 14:28:22 +000073
Benjamin Petersone41251e2008-04-25 01:59:09 +000074 Contains the request path.
Georg Brandl116aa622007-08-15 14:28:22 +000075
76
Benjamin Petersone41251e2008-04-25 01:59:09 +000077 .. attribute:: request_version
Georg Brandl116aa622007-08-15 14:28:22 +000078
Benjamin Petersone41251e2008-04-25 01:59:09 +000079 Contains the version string from the request. For example, ``'HTTP/1.0'``.
Georg Brandl116aa622007-08-15 14:28:22 +000080
81
Benjamin Petersone41251e2008-04-25 01:59:09 +000082 .. attribute:: headers
Georg Brandl116aa622007-08-15 14:28:22 +000083
Benjamin Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +000087
88
Benjamin Petersone41251e2008-04-25 01:59:09 +000089 .. attribute:: rfile
Georg Brandl116aa622007-08-15 14:28:22 +000090
Benjamin Petersone41251e2008-04-25 01:59:09 +000091 Contains an input stream, positioned at the start of the optional input
92 data.
Georg Brandl116aa622007-08-15 14:28:22 +000093
94
Benjamin Petersone41251e2008-04-25 01:59:09 +000095 .. attribute:: wfile
Georg Brandl116aa622007-08-15 14:28:22 +000096
Benjamin Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +0000100
Georg Brandl116aa622007-08-15 14:28:22 +0000101
Benjamin Petersone41251e2008-04-25 01:59:09 +0000102 :class:`BaseHTTPRequestHandler` has the following class variables:
Georg Brandl116aa622007-08-15 14:28:22 +0000103
Georg Brandl116aa622007-08-15 14:28:22 +0000104
Benjamin Petersone41251e2008-04-25 01:59:09 +0000105 .. attribute:: server_version
Georg Brandl116aa622007-08-15 14:28:22 +0000106
Benjamin Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +0000110
Georg Brandl116aa622007-08-15 14:28:22 +0000111
Benjamin Petersone41251e2008-04-25 01:59:09 +0000112 .. attribute:: sys_version
Georg Brandl116aa622007-08-15 14:28:22 +0000113
Benjamin Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +0000117
Georg Brandl116aa622007-08-15 14:28:22 +0000118
Benjamin Petersone41251e2008-04-25 01:59:09 +0000119 .. attribute:: error_message_format
Georg Brandl116aa622007-08-15 14:28:22 +0000120
Benjamin Petersone41251e2008-04-25 01:59:09 +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 Brandl116aa622007-08-15 14:28:22 +0000128
Christian Heimes8640e742008-02-23 16:23:06 +0000129
Benjamin Petersone41251e2008-04-25 01:59:09 +0000130 .. attribute:: error_content_type
Christian Heimes8640e742008-02-23 16:23:06 +0000131
Benjamin Petersone41251e2008-04-25 01:59:09 +0000132 Specifies the Content-Type HTTP header of error responses sent to the
133 client. The default value is ``'text/html'``.
Christian Heimes8640e742008-02-23 16:23:06 +0000134
Georg Brandl116aa622007-08-15 14:28:22 +0000135
Benjamin Petersone41251e2008-04-25 01:59:09 +0000136 .. attribute:: protocol_version
Georg Brandl116aa622007-08-15 14:28:22 +0000137
Benjamin Petersone41251e2008-04-25 01:59:09 +0000138 This specifies the HTTP protocol version used in responses. If set to
139 ``'HTTP/1.1'``, the server will permit HTTP persistent connections;
140 however, your server *must* then include an accurate ``Content-Length``
141 header (using :meth:`send_header`) in all of its responses to clients.
142 For backwards compatibility, the setting defaults to ``'HTTP/1.0'``.
Georg Brandl116aa622007-08-15 14:28:22 +0000143
Georg Brandl116aa622007-08-15 14:28:22 +0000144
Benjamin Petersone41251e2008-04-25 01:59:09 +0000145 .. attribute:: MessageClass
146
147 .. index:: single: Message (in module mimetools)
148
149 Specifies a :class:`rfc822.Message`\ -like class to parse HTTP headers.
150 Typically, this is not overridden, and it defaults to
151 :class:`mimetools.Message`.
Georg Brandl116aa622007-08-15 14:28:22 +0000152
Georg Brandl116aa622007-08-15 14:28:22 +0000153
Benjamin Petersone41251e2008-04-25 01:59:09 +0000154 .. attribute:: responses
Georg Brandl116aa622007-08-15 14:28:22 +0000155
Benjamin Petersone41251e2008-04-25 01:59:09 +0000156 This variable contains a mapping of error code integers to two-element tuples
157 containing a short and long message. For example, ``{code: (shortmessage,
158 longmessage)}``. The *shortmessage* is usually used as the *message* key in an
159 error response, and *longmessage* as the *explain* key (see the
160 :attr:`error_message_format` class variable).
Georg Brandl116aa622007-08-15 14:28:22 +0000161
Georg Brandl116aa622007-08-15 14:28:22 +0000162
Benjamin Petersone41251e2008-04-25 01:59:09 +0000163 A :class:`BaseHTTPRequestHandler` instance has the following methods:
Georg Brandl116aa622007-08-15 14:28:22 +0000164
165
Benjamin Petersone41251e2008-04-25 01:59:09 +0000166 .. method:: handle()
Georg Brandl116aa622007-08-15 14:28:22 +0000167
Benjamin Petersone41251e2008-04-25 01:59:09 +0000168 Calls :meth:`handle_one_request` once (or, if persistent connections are
169 enabled, multiple times) to handle incoming HTTP requests. You should
170 never need to override it; instead, implement appropriate :meth:`do_\*`
171 methods.
Georg Brandl116aa622007-08-15 14:28:22 +0000172
173
Benjamin Petersone41251e2008-04-25 01:59:09 +0000174 .. method:: handle_one_request()
Georg Brandl116aa622007-08-15 14:28:22 +0000175
Benjamin Petersone41251e2008-04-25 01:59:09 +0000176 This method will parse and dispatch the request to the appropriate
177 :meth:`do_\*` method. You should never need to override it.
Georg Brandl116aa622007-08-15 14:28:22 +0000178
179
Benjamin Petersone41251e2008-04-25 01:59:09 +0000180 .. method:: send_error(code[, message])
Georg Brandl116aa622007-08-15 14:28:22 +0000181
Benjamin Petersone41251e2008-04-25 01:59:09 +0000182 Sends and logs a complete error reply to the client. The numeric *code*
183 specifies the HTTP error code, with *message* as optional, more specific text. A
184 complete set of headers is sent, followed by text composed using the
185 :attr:`error_message_format` class variable.
Georg Brandl116aa622007-08-15 14:28:22 +0000186
187
Benjamin Petersone41251e2008-04-25 01:59:09 +0000188 .. method:: send_response(code[, message])
Georg Brandl116aa622007-08-15 14:28:22 +0000189
Benjamin Petersone41251e2008-04-25 01:59:09 +0000190 Sends a response header and logs the accepted request. The HTTP response
191 line is sent, followed by *Server* and *Date* headers. The values for
192 these two headers are picked up from the :meth:`version_string` and
193 :meth:`date_time_string` methods, respectively.
Georg Brandl116aa622007-08-15 14:28:22 +0000194
195
Benjamin Petersone41251e2008-04-25 01:59:09 +0000196 .. method:: send_header(keyword, value)
Georg Brandl116aa622007-08-15 14:28:22 +0000197
Benjamin Petersone41251e2008-04-25 01:59:09 +0000198 Writes a specific HTTP header to the output stream. *keyword* should
199 specify the header keyword, with *value* specifying its value.
Georg Brandl116aa622007-08-15 14:28:22 +0000200
201
Benjamin Petersone41251e2008-04-25 01:59:09 +0000202 .. method:: end_headers()
Georg Brandl116aa622007-08-15 14:28:22 +0000203
Benjamin Petersone41251e2008-04-25 01:59:09 +0000204 Sends a blank line, indicating the end of the HTTP headers in the
205 response.
Georg Brandl116aa622007-08-15 14:28:22 +0000206
207
Benjamin Petersone41251e2008-04-25 01:59:09 +0000208 .. method:: log_request([code[, size]])
Georg Brandl116aa622007-08-15 14:28:22 +0000209
Benjamin Petersone41251e2008-04-25 01:59:09 +0000210 Logs an accepted (successful) request. *code* should specify the numeric
211 HTTP code associated with the response. If a size of the response is
212 available, then it should be passed as the *size* parameter.
Georg Brandl116aa622007-08-15 14:28:22 +0000213
214
Benjamin Petersone41251e2008-04-25 01:59:09 +0000215 .. method:: log_error(...)
Georg Brandl116aa622007-08-15 14:28:22 +0000216
Benjamin Petersone41251e2008-04-25 01:59:09 +0000217 Logs an error when a request cannot be fulfilled. By default, it passes
218 the message to :meth:`log_message`, so it takes the same arguments
219 (*format* and additional values).
Georg Brandl116aa622007-08-15 14:28:22 +0000220
221
Benjamin Petersone41251e2008-04-25 01:59:09 +0000222 .. method:: log_message(format, ...)
Georg Brandl116aa622007-08-15 14:28:22 +0000223
Benjamin Petersone41251e2008-04-25 01:59:09 +0000224 Logs an arbitrary message to ``sys.stderr``. This is typically overridden
225 to create custom error logging mechanisms. The *format* argument is a
226 standard printf-style format string, where the additional arguments to
227 :meth:`log_message` are applied as inputs to the formatting. The client
228 address and current date and time are prefixed to every message logged.
Georg Brandl116aa622007-08-15 14:28:22 +0000229
230
Benjamin Petersone41251e2008-04-25 01:59:09 +0000231 .. method:: version_string()
Georg Brandl116aa622007-08-15 14:28:22 +0000232
Benjamin Petersone41251e2008-04-25 01:59:09 +0000233 Returns the server software's version string. This is a combination of the
234 :attr:`server_version` and :attr:`sys_version` class variables.
Georg Brandl116aa622007-08-15 14:28:22 +0000235
236
Benjamin Petersone41251e2008-04-25 01:59:09 +0000237 .. method:: date_time_string([timestamp])
Georg Brandl116aa622007-08-15 14:28:22 +0000238
Benjamin Petersone41251e2008-04-25 01:59:09 +0000239 Returns the date and time given by *timestamp* (which must be in the
240 format returned by :func:`time.time`), formatted for a message header. If
241 *timestamp* is omitted, it uses the current date and time.
Georg Brandl116aa622007-08-15 14:28:22 +0000242
Benjamin Petersone41251e2008-04-25 01:59:09 +0000243 The result looks like ``'Sun, 06 Nov 1994 08:49:37 GMT'``.
Georg Brandl116aa622007-08-15 14:28:22 +0000244
Georg Brandl116aa622007-08-15 14:28:22 +0000245
Benjamin Petersone41251e2008-04-25 01:59:09 +0000246 .. method:: log_date_time_string()
Georg Brandl116aa622007-08-15 14:28:22 +0000247
Benjamin Petersone41251e2008-04-25 01:59:09 +0000248 Returns the current date and time, formatted for logging.
Georg Brandl116aa622007-08-15 14:28:22 +0000249
250
Benjamin Petersone41251e2008-04-25 01:59:09 +0000251 .. method:: address_string()
Georg Brandl116aa622007-08-15 14:28:22 +0000252
Benjamin Petersone41251e2008-04-25 01:59:09 +0000253 Returns the client address, formatted for logging. A name lookup is
254 performed on the client's IP address.
Georg Brandl116aa622007-08-15 14:28:22 +0000255
256
257.. seealso::
258
259 Module :mod:`CGIHTTPServer`
260 Extended request handler that supports CGI scripts.
261
262 Module :mod:`SimpleHTTPServer`
263 Basic request handler that limits response to files actually under the document
264 root.
265