blob: 941f04321bce2cb76f2b5039b7dfb6cd35660241 [file] [log] [blame]
Georg Brandl24420152008-05-26 16:32:26 +00001:mod:`http.server` --- HTTP servers
2===================================
3
4.. module:: http.server
5 :synopsis: HTTP server and request handlers.
6
7
8.. index::
9 pair: WWW; server
10 pair: HTTP; protocol
11 single: URL
12 single: httpd
13
14This module defines classes for implementing HTTP servers (Web servers).
15
16One class, :class:`HTTPServer`, is a :class:`socketserver.TCPServer` subclass.
17It creates and listens at the HTTP socket, dispatching the requests to a
18handler. Code to create and run the server looks like this::
19
20 def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler):
21 server_address = ('', 8000)
22 httpd = server_class(server_address, handler_class)
23 httpd.serve_forever()
24
25
26.. class:: HTTPServer(server_address, RequestHandlerClass)
27
28 This class builds on the :class:`TCPServer` class by storing the server
29 address as instance variables named :attr:`server_name` and
30 :attr:`server_port`. The server is accessible by the handler, typically
31 through the handler's :attr:`server` instance variable.
32
33
34The :class:`HTTPServer` must be given a *RequestHandlerClass* on instantiation,
35of which this module provides three different variants:
36
37.. class:: BaseHTTPRequestHandler(request, client_address, server)
38
39 This class is used to handle the HTTP requests that arrive at the server. By
40 itself, it cannot respond to any actual HTTP requests; it must be subclassed
41 to handle each request method (e.g. GET or POST).
42 :class:`BaseHTTPRequestHandler` provides a number of class and instance
43 variables, and methods for use by subclasses.
44
45 The handler will parse the request and the headers, then call a method
46 specific to the request type. The method name is constructed from the
47 request. For example, for the request method ``SPAM``, the :meth:`do_SPAM`
48 method will be called with no arguments. All of the relevant information is
49 stored in instance variables of the handler. Subclasses should not need to
50 override or extend the :meth:`__init__` method.
51
52 :class:`BaseHTTPRequestHandler` has the following instance variables:
53
54 .. attribute:: client_address
55
56 Contains a tuple of the form ``(host, port)`` referring to the client's
57 address.
58
59 .. attribute:: command
60
61 Contains the command (request type). For example, ``'GET'``.
62
63 .. attribute:: path
64
65 Contains the request path.
66
67 .. attribute:: request_version
68
69 Contains the version string from the request. For example, ``'HTTP/1.0'``.
70
71 .. attribute:: headers
72
73 Holds an instance of the class specified by the :attr:`MessageClass` class
74 variable. This instance parses and manages the headers in the HTTP
75 request.
76
77 .. attribute:: rfile
78
79 Contains an input stream, positioned at the start of the optional input
80 data.
81
82 .. attribute:: wfile
83
84 Contains the output stream for writing a response back to the
85 client. Proper adherence to the HTTP protocol must be used when writing to
86 this stream.
87
88 :class:`BaseHTTPRequestHandler` has the following class variables:
89
90 .. attribute:: server_version
91
92 Specifies the server software version. You may want to override this. The
93 format is multiple whitespace-separated strings, where each string is of
94 the form name[/version]. For example, ``'BaseHTTP/0.2'``.
95
96 .. attribute:: sys_version
97
98 Contains the Python system version, in a form usable by the
99 :attr:`version_string` method and the :attr:`server_version` class
100 variable. For example, ``'Python/1.4'``.
101
102 .. attribute:: error_message_format
103
104 Specifies a format string for building an error response to the client. It
105 uses parenthesized, keyed format specifiers, so the format operand must be
106 a dictionary. The *code* key should be an integer, specifying the numeric
107 HTTP error code value. *message* should be a string containing a
108 (detailed) error message of what occurred, and *explain* should be an
109 explanation of the error code number. Default *message* and *explain*
110 values can found in the *responses* class variable.
111
112 .. attribute:: error_content_type
113
114 Specifies the Content-Type HTTP header of error responses sent to the
115 client. The default value is ``'text/html'``.
116
117 .. attribute:: protocol_version
118
119 This specifies the HTTP protocol version used in responses. If set to
120 ``'HTTP/1.1'``, the server will permit HTTP persistent connections;
121 however, your server *must* then include an accurate ``Content-Length``
122 header (using :meth:`send_header`) in all of its responses to clients.
123 For backwards compatibility, the setting defaults to ``'HTTP/1.0'``.
124
125 .. attribute:: MessageClass
126
127 .. index:: single: Message (in module mimetools)
128
129 Specifies a :class:`rfc822.Message`\ -like class to parse HTTP headers.
130 Typically, this is not overridden, and it defaults to
131 :class:`mimetools.Message`.
132
133 .. attribute:: responses
134
135 This variable contains a mapping of error code integers to two-element tuples
136 containing a short and long message. For example, ``{code: (shortmessage,
137 longmessage)}``. The *shortmessage* is usually used as the *message* key in an
138 error response, and *longmessage* as the *explain* key (see the
139 :attr:`error_message_format` class variable).
140
141 A :class:`BaseHTTPRequestHandler` instance has the following methods:
142
143 .. method:: handle()
144
145 Calls :meth:`handle_one_request` once (or, if persistent connections are
146 enabled, multiple times) to handle incoming HTTP requests. You should
147 never need to override it; instead, implement appropriate :meth:`do_\*`
148 methods.
149
150 .. method:: handle_one_request()
151
152 This method will parse and dispatch the request to the appropriate
153 :meth:`do_\*` method. You should never need to override it.
154
155 .. method:: send_error(code[, message])
156
157 Sends and logs a complete error reply to the client. The numeric *code*
158 specifies the HTTP error code, with *message* as optional, more specific text. A
159 complete set of headers is sent, followed by text composed using the
160 :attr:`error_message_format` class variable.
161
162 .. method:: send_response(code[, message])
163
164 Sends a response header and logs the accepted request. The HTTP response
165 line is sent, followed by *Server* and *Date* headers. The values for
166 these two headers are picked up from the :meth:`version_string` and
167 :meth:`date_time_string` methods, respectively.
168
169 .. method:: send_header(keyword, value)
170
171 Writes a specific HTTP header to the output stream. *keyword* should
172 specify the header keyword, with *value* specifying its value.
173
174 .. method:: end_headers()
175
176 Sends a blank line, indicating the end of the HTTP headers in the
177 response.
178
179 .. method:: log_request([code[, size]])
180
181 Logs an accepted (successful) request. *code* should specify the numeric
182 HTTP code associated with the response. If a size of the response is
183 available, then it should be passed as the *size* parameter.
184
185 .. method:: log_error(...)
186
187 Logs an error when a request cannot be fulfilled. By default, it passes
188 the message to :meth:`log_message`, so it takes the same arguments
189 (*format* and additional values).
190
191
192 .. method:: log_message(format, ...)
193
194 Logs an arbitrary message to ``sys.stderr``. This is typically overridden
195 to create custom error logging mechanisms. The *format* argument is a
196 standard printf-style format string, where the additional arguments to
197 :meth:`log_message` are applied as inputs to the formatting. The client
198 address and current date and time are prefixed to every message logged.
199
200 .. method:: version_string()
201
202 Returns the server software's version string. This is a combination of the
203 :attr:`server_version` and :attr:`sys_version` class variables.
204
205 .. method:: date_time_string([timestamp])
206
207 Returns the date and time given by *timestamp* (which must be in the
208 format returned by :func:`time.time`), formatted for a message header. If
209 *timestamp* is omitted, it uses the current date and time.
210
211 The result looks like ``'Sun, 06 Nov 1994 08:49:37 GMT'``.
212
213 .. method:: log_date_time_string()
214
215 Returns the current date and time, formatted for logging.
216
217 .. method:: address_string()
218
219 Returns the client address, formatted for logging. A name lookup is
220 performed on the client's IP address.
221
222
223.. class:: SimpleHTTPRequestHandler(request, client_address, server)
224
225 This class serves files from the current directory and below, directly
226 mapping the directory structure to HTTP requests.
227
228 A lot of the work, such as parsing the request, is done by the base class
229 :class:`BaseHTTPRequestHandler`. This class implements the :func:`do_GET`
230 and :func:`do_HEAD` functions.
231
232 The following are defined as class-level attributes of
233 :class:`SimpleHTTPRequestHandler`:
234
235 .. attribute:: server_version
236
237 This will be ``"SimpleHTTP/" + __version__``, where ``__version__`` is
238 defined at the module level.
239
240 .. attribute:: extensions_map
241
242 A dictionary mapping suffixes into MIME types. The default is
243 signified by an empty string, and is considered to be
244 ``application/octet-stream``. The mapping is used case-insensitively,
245 and so should contain only lower-cased keys.
246
247 The :class:`SimpleHTTPRequestHandler` class defines the following methods:
248
249 .. method:: do_HEAD()
250
251 This method serves the ``'HEAD'`` request type: it sends the headers it
252 would send for the equivalent ``GET`` request. See the :meth:`do_GET`
253 method for a more complete explanation of the possible headers.
254
255 .. method:: do_GET()
256
257 The request is mapped to a local file by interpreting the request as a
258 path relative to the current working directory.
259
260 If the request was mapped to a directory, the directory is checked for a
261 file named ``index.html`` or ``index.htm`` (in that order). If found, the
262 file's contents are returned; otherwise a directory listing is generated
263 by calling the :meth:`list_directory` method. This method uses
264 :func:`os.listdir` to scan the directory, and returns a ``404`` error
265 response if the :func:`listdir` fails.
266
267 If the request was mapped to a file, it is opened and the contents are
268 returned. Any :exc:`IOError` exception in opening the requested file is
269 mapped to a ``404``, ``'File not found'`` error. Otherwise, the content
270 type is guessed by calling the :meth:`guess_type` method, which in turn
271 uses the *extensions_map* variable.
272
273 A ``'Content-type:'`` header with the guessed content type is output,
274 followed by a ``'Content-Length:'`` header with the file's size and a
275 ``'Last-Modified:'`` header with the file's modification time.
276
277 Then follows a blank line signifying the end of the headers, and then the
278 contents of the file are output. If the file's MIME type starts with
279 ``text/`` the file is opened in text mode; otherwise binary mode is used.
280
281 For example usage, see the implementation of the :func:`test` function.
282
283
284.. class:: CGIHTTPRequestHandler(request, client_address, server)
285
286 This class is used to serve either files or output of CGI scripts from the
287 current directory and below. Note that mapping HTTP hierarchic structure to
288 local directory structure is exactly as in :class:`SimpleHTTPRequestHandler`.
289
290 .. note::
291
292 CGI scripts run by the :class:`CGIHTTPRequestHandler` class cannot execute
293 redirects (HTTP code 302), because code 200 (script output follows) is
294 sent prior to execution of the CGI script. This pre-empts the status
295 code.
296
297 The class will however, run the CGI script, instead of serving it as a file,
298 if it guesses it to be a CGI script. Only directory-based CGI are used ---
299 the other common server configuration is to treat special extensions as
300 denoting CGI scripts.
301
302 The :func:`do_GET` and :func:`do_HEAD` functions are modified to run CGI scripts
303 and serve the output, instead of serving files, if the request leads to
304 somewhere below the ``cgi_directories`` path.
305
306 The :class:`CGIHTTPRequestHandler` defines the following data member:
307
308 .. attribute:: cgi_directories
309
310 This defaults to ``['/cgi-bin', '/htbin']`` and describes directories to
311 treat as containing CGI scripts.
312
313 The :class:`CGIHTTPRequestHandler` defines the following method:
314
315 .. method:: do_POST()
316
317 This method serves the ``'POST'`` request type, only allowed for CGI
318 scripts. Error 501, "Can only POST to CGI scripts", is output when trying
319 to POST to a non-CGI url.
320
321 Note that CGI scripts will be run with UID of user nobody, for security
322 reasons. Problems with the CGI script will be translated to error 403.