blob: c922d2f53558d72ca7317f92b08f1f907d9c51b6 [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
Benjamin Petersonc7b05922008-04-25 01:29:10 +000071 .. attribute:: command
Georg Brandl8ec7f652007-08-15 14:28:01 +000072
Benjamin Petersonc7b05922008-04-25 01:29:10 +000073 Contains the command (request type). For example, ``'GET'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000074
75
Benjamin Petersonc7b05922008-04-25 01:29:10 +000076 .. attribute:: path
Georg Brandl8ec7f652007-08-15 14:28:01 +000077
Benjamin Petersonc7b05922008-04-25 01:29:10 +000078 Contains the request path.
Georg Brandl8ec7f652007-08-15 14:28:01 +000079
80
Benjamin Petersonc7b05922008-04-25 01:29:10 +000081 .. attribute:: request_version
Georg Brandl8ec7f652007-08-15 14:28:01 +000082
Benjamin Petersonc7b05922008-04-25 01:29:10 +000083 Contains the version string from the request. For example, ``'HTTP/1.0'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +000084
85
Benjamin Petersonc7b05922008-04-25 01:29:10 +000086 .. attribute:: headers
Georg Brandl8ec7f652007-08-15 14:28:01 +000087
Benjamin Petersonc7b05922008-04-25 01:29:10 +000088 Holds an instance of the class specified by the :attr:`MessageClass` class
89 variable. This instance parses and manages the headers in the HTTP
90 request.
Georg Brandl8ec7f652007-08-15 14:28:01 +000091
92
Benjamin Petersonc7b05922008-04-25 01:29:10 +000093 .. attribute:: rfile
Georg Brandl8ec7f652007-08-15 14:28:01 +000094
Benjamin Petersonc7b05922008-04-25 01:29:10 +000095 Contains an input stream, positioned at the start of the optional input
96 data.
Georg Brandl8ec7f652007-08-15 14:28:01 +000097
98
Benjamin Petersonc7b05922008-04-25 01:29:10 +000099 .. attribute:: wfile
Georg Brandl8ec7f652007-08-15 14:28:01 +0000100
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000101 Contains the output stream for writing a response back to the
102 client. Proper adherence to the HTTP protocol must be used when writing to
103 this stream.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000104
Georg Brandl8ec7f652007-08-15 14:28:01 +0000105
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000106 :class:`BaseHTTPRequestHandler` has the following class variables:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000107
Georg Brandl8ec7f652007-08-15 14:28:01 +0000108
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000109 .. attribute:: server_version
Georg Brandl8ec7f652007-08-15 14:28:01 +0000110
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000111 Specifies the server software version. You may want to override this. The
112 format is multiple whitespace-separated strings, where each string is of
113 the form name[/version]. For example, ``'BaseHTTP/0.2'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000114
Georg Brandl8ec7f652007-08-15 14:28:01 +0000115
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000116 .. attribute:: sys_version
Georg Brandl8ec7f652007-08-15 14:28:01 +0000117
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000118 Contains the Python system version, in a form usable by the
119 :attr:`version_string` method and the :attr:`server_version` class
120 variable. For example, ``'Python/1.4'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000121
Georg Brandl8ec7f652007-08-15 14:28:01 +0000122
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000123 .. attribute:: error_message_format
Georg Brandl8ec7f652007-08-15 14:28:01 +0000124
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000125 Specifies a format string for building an error response to the client. It
126 uses parenthesized, keyed format specifiers, so the format operand must be
127 a dictionary. The *code* key should be an integer, specifying the numeric
128 HTTP error code value. *message* should be a string containing a
129 (detailed) error message of what occurred, and *explain* should be an
130 explanation of the error code number. Default *message* and *explain*
131 values can found in the *responses* class variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000132
Georg Brandl16479232008-02-23 15:02:28 +0000133
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000134 .. attribute:: error_content_type
Georg Brandl16479232008-02-23 15:02:28 +0000135
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000136 Specifies the Content-Type HTTP header of error responses sent to the
137 client. The default value is ``'text/html'``.
Georg Brandl16479232008-02-23 15:02:28 +0000138
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000139 .. versionadded:: 2.6
140 Previously, the content type was always ``'text/html'``.
Georg Brandl16479232008-02-23 15:02:28 +0000141
Georg Brandl8ec7f652007-08-15 14:28:01 +0000142
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000143 .. attribute:: protocol_version
Georg Brandl8ec7f652007-08-15 14:28:01 +0000144
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000145 This specifies the HTTP protocol version used in responses. If set to
146 ``'HTTP/1.1'``, the server will permit HTTP persistent connections;
147 however, your server *must* then include an accurate ``Content-Length``
148 header (using :meth:`send_header`) in all of its responses to clients.
149 For backwards compatibility, the setting defaults to ``'HTTP/1.0'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000150
Georg Brandl8ec7f652007-08-15 14:28:01 +0000151
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000152 .. attribute:: MessageClass
153
154 .. index:: single: Message (in module mimetools)
155
156 Specifies a :class:`rfc822.Message`\ -like class to parse HTTP headers.
157 Typically, this is not overridden, and it defaults to
158 :class:`mimetools.Message`.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000159
Georg Brandl8ec7f652007-08-15 14:28:01 +0000160
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000161 .. attribute:: responses
Georg Brandl8ec7f652007-08-15 14:28:01 +0000162
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000163 This variable contains a mapping of error code integers to two-element tuples
164 containing a short and long message. For example, ``{code: (shortmessage,
165 longmessage)}``. The *shortmessage* is usually used as the *message* key in an
166 error response, and *longmessage* as the *explain* key (see the
167 :attr:`error_message_format` class variable).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000168
Georg Brandl8ec7f652007-08-15 14:28:01 +0000169
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000170 A :class:`BaseHTTPRequestHandler` instance has the following methods:
Georg Brandl8ec7f652007-08-15 14:28:01 +0000171
172
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000173 .. method:: handle()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000174
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000175 Calls :meth:`handle_one_request` once (or, if persistent connections are
176 enabled, multiple times) to handle incoming HTTP requests. You should
177 never need to override it; instead, implement appropriate :meth:`do_\*`
178 methods.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000179
180
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000181 .. method:: handle_one_request()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000182
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000183 This method will parse and dispatch the request to the appropriate
184 :meth:`do_\*` method. You should never need to override it.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000185
186
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000187 .. method:: send_error(code[, message])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000188
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000189 Sends and logs a complete error reply to the client. The numeric *code*
190 specifies the HTTP error code, with *message* as optional, more specific text. A
191 complete set of headers is sent, followed by text composed using the
192 :attr:`error_message_format` class variable.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000193
194
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000195 .. method:: send_response(code[, message])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000196
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000197 Sends a response header and logs the accepted request. The HTTP response
198 line is sent, followed by *Server* and *Date* headers. The values for
199 these two headers are picked up from the :meth:`version_string` and
200 :meth:`date_time_string` methods, respectively.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000201
202
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000203 .. method:: send_header(keyword, value)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000204
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000205 Writes a specific HTTP header to the output stream. *keyword* should
206 specify the header keyword, with *value* specifying its value.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000207
208
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000209 .. method:: end_headers()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000210
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000211 Sends a blank line, indicating the end of the HTTP headers in the
212 response.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000213
214
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000215 .. method:: log_request([code[, size]])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000216
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000217 Logs an accepted (successful) request. *code* should specify the numeric
218 HTTP code associated with the response. If a size of the response is
219 available, then it should be passed as the *size* parameter.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000220
221
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000222 .. method:: log_error(...)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000223
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000224 Logs an error when a request cannot be fulfilled. By default, it passes
225 the message to :meth:`log_message`, so it takes the same arguments
226 (*format* and additional values).
Georg Brandl8ec7f652007-08-15 14:28:01 +0000227
228
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000229 .. method:: log_message(format, ...)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000230
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000231 Logs an arbitrary message to ``sys.stderr``. This is typically overridden
232 to create custom error logging mechanisms. The *format* argument is a
233 standard printf-style format string, where the additional arguments to
234 :meth:`log_message` are applied as inputs to the formatting. The client
235 address and current date and time are prefixed to every message logged.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000236
237
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000238 .. method:: version_string()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000239
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000240 Returns the server software's version string. This is a combination of the
241 :attr:`server_version` and :attr:`sys_version` class variables.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000242
243
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000244 .. method:: date_time_string([timestamp])
Georg Brandl8ec7f652007-08-15 14:28:01 +0000245
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000246 Returns the date and time given by *timestamp* (which must be in the
247 format returned by :func:`time.time`), formatted for a message header. If
248 *timestamp* is omitted, it uses the current date and time.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000249
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000250 The result looks like ``'Sun, 06 Nov 1994 08:49:37 GMT'``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000251
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000252 .. versionadded:: 2.5
253 The *timestamp* parameter.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000254
255
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000256 .. method:: log_date_time_string()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000257
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000258 Returns the current date and time, formatted for logging.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000259
260
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000261 .. method:: address_string()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000262
Benjamin Petersonc7b05922008-04-25 01:29:10 +0000263 Returns the client address, formatted for logging. A name lookup is
264 performed on the client's IP address.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000265
266
267.. seealso::
268
269 Module :mod:`CGIHTTPServer`
270 Extended request handler that supports CGI scripts.
271
272 Module :mod:`SimpleHTTPServer`
273 Basic request handler that limits response to files actually under the document
274 root.
275