blob: f68d2207d44156eabc001d4f04836816006f0eac [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
Benjamin Peterson3e4f0552008-09-02 00:31:15 +000059 .. attribute:: server
60
61 Contains the server instance.
62
63
Georg Brandl24420152008-05-26 16:32:26 +000064 .. attribute:: command
65
66 Contains the command (request type). For example, ``'GET'``.
67
68 .. attribute:: path
69
70 Contains the request path.
71
72 .. attribute:: request_version
73
74 Contains the version string from the request. For example, ``'HTTP/1.0'``.
75
76 .. attribute:: headers
77
78 Holds an instance of the class specified by the :attr:`MessageClass` class
79 variable. This instance parses and manages the headers in the HTTP
80 request.
81
82 .. attribute:: rfile
83
84 Contains an input stream, positioned at the start of the optional input
85 data.
86
87 .. attribute:: wfile
88
89 Contains the output stream for writing a response back to the
90 client. Proper adherence to the HTTP protocol must be used when writing to
91 this stream.
92
93 :class:`BaseHTTPRequestHandler` has the following class variables:
94
95 .. attribute:: server_version
96
97 Specifies the server software version. You may want to override this. The
98 format is multiple whitespace-separated strings, where each string is of
99 the form name[/version]. For example, ``'BaseHTTP/0.2'``.
100
101 .. attribute:: sys_version
102
103 Contains the Python system version, in a form usable by the
104 :attr:`version_string` method and the :attr:`server_version` class
105 variable. For example, ``'Python/1.4'``.
106
107 .. attribute:: error_message_format
108
109 Specifies a format string for building an error response to the client. It
110 uses parenthesized, keyed format specifiers, so the format operand must be
111 a dictionary. The *code* key should be an integer, specifying the numeric
112 HTTP error code value. *message* should be a string containing a
113 (detailed) error message of what occurred, and *explain* should be an
114 explanation of the error code number. Default *message* and *explain*
115 values can found in the *responses* class variable.
116
117 .. attribute:: error_content_type
118
119 Specifies the Content-Type HTTP header of error responses sent to the
120 client. The default value is ``'text/html'``.
121
122 .. attribute:: protocol_version
123
124 This specifies the HTTP protocol version used in responses. If set to
125 ``'HTTP/1.1'``, the server will permit HTTP persistent connections;
126 however, your server *must* then include an accurate ``Content-Length``
127 header (using :meth:`send_header`) in all of its responses to clients.
128 For backwards compatibility, the setting defaults to ``'HTTP/1.0'``.
129
130 .. attribute:: MessageClass
131
Georg Brandl83e9f4c2008-06-12 18:52:31 +0000132 Specifies an :class:`email.message.Message`\ -like class to parse HTTP
133 headers. Typically, this is not overridden, and it defaults to
134 :class:`http.client.HTTPMessage`.
Georg Brandl24420152008-05-26 16:32:26 +0000135
136 .. attribute:: responses
137
138 This variable contains a mapping of error code integers to two-element tuples
139 containing a short and long message. For example, ``{code: (shortmessage,
140 longmessage)}``. The *shortmessage* is usually used as the *message* key in an
141 error response, and *longmessage* as the *explain* key (see the
142 :attr:`error_message_format` class variable).
143
144 A :class:`BaseHTTPRequestHandler` instance has the following methods:
145
146 .. method:: handle()
147
148 Calls :meth:`handle_one_request` once (or, if persistent connections are
149 enabled, multiple times) to handle incoming HTTP requests. You should
150 never need to override it; instead, implement appropriate :meth:`do_\*`
151 methods.
152
153 .. method:: handle_one_request()
154
155 This method will parse and dispatch the request to the appropriate
156 :meth:`do_\*` method. You should never need to override it.
157
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000158 .. method:: handle_expect_100()
159
160 When a HTTP/1.1 compliant server receives a ``Expect: 100-continue``
161 request header it responds back with a ``100 Continue`` followed by ``200
162 OK`` headers.
163 This method can be overridden to raise an error if the server does not
164 want the client to continue. For e.g. server can chose to send ``417
165 Expectation Failed`` as a response header and ``return False``.
166
167 .. versionadded:: 3.2
168
Georg Brandl036490d2009-05-17 13:00:36 +0000169 .. method:: send_error(code, message=None)
Georg Brandl24420152008-05-26 16:32:26 +0000170
171 Sends and logs a complete error reply to the client. The numeric *code*
172 specifies the HTTP error code, with *message* as optional, more specific text. A
173 complete set of headers is sent, followed by text composed using the
174 :attr:`error_message_format` class variable.
175
Georg Brandl036490d2009-05-17 13:00:36 +0000176 .. method:: send_response(code, message=None)
Georg Brandl24420152008-05-26 16:32:26 +0000177
178 Sends a response header and logs the accepted request. The HTTP response
179 line is sent, followed by *Server* and *Date* headers. The values for
180 these two headers are picked up from the :meth:`version_string` and
181 :meth:`date_time_string` methods, respectively.
182
183 .. method:: send_header(keyword, value)
184
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000185 Stores the HTTP header to an internal buffer which will be written to the
186 output stream when :meth:`end_headers` method is invoked.
187 *keyword* should specify the header keyword, with *value*
188 specifying its value.
189
190 .. versionchanged:: 3.2 Storing the headers in an internal buffer
191
Georg Brandl24420152008-05-26 16:32:26 +0000192
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000193 .. method:: send_response_only(code, message=None)
194
195 Sends the reponse header only, used for the purposes when ``100
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000196 Continue`` response is sent by the server to the client. The headers not
197 buffered and sent directly the output stream.If the *message* is not
198 specified, the HTTP message corresponding the response *code* is sent.
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000199
200 .. versionadded:: 3.2
201
Georg Brandl24420152008-05-26 16:32:26 +0000202 .. method:: end_headers()
203
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000204 Write the buffered HTTP headers to the output stream and send a blank
205 line, indicating the end of the HTTP headers in the response.
206
207 .. versionchanged:: 3.2 Writing the buffered headers to the output stream.
Georg Brandl24420152008-05-26 16:32:26 +0000208
Georg Brandl036490d2009-05-17 13:00:36 +0000209 .. method:: log_request(code='-', size='-')
Georg Brandl24420152008-05-26 16:32:26 +0000210
211 Logs an accepted (successful) request. *code* should specify the numeric
212 HTTP code associated with the response. If a size of the response is
213 available, then it should be passed as the *size* parameter.
214
215 .. method:: log_error(...)
216
217 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).
220
221
222 .. method:: log_message(format, ...)
223
224 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.
229
230 .. method:: version_string()
231
232 Returns the server software's version string. This is a combination of the
233 :attr:`server_version` and :attr:`sys_version` class variables.
234
Georg Brandl036490d2009-05-17 13:00:36 +0000235 .. method:: date_time_string(timestamp=None)
Georg Brandl24420152008-05-26 16:32:26 +0000236
Georg Brandl036490d2009-05-17 13:00:36 +0000237 Returns the date and time given by *timestamp* (which must be None or in
238 the format returned by :func:`time.time`), formatted for a message
239 header. If *timestamp* is omitted, it uses the current date and time.
Georg Brandl24420152008-05-26 16:32:26 +0000240
241 The result looks like ``'Sun, 06 Nov 1994 08:49:37 GMT'``.
242
243 .. method:: log_date_time_string()
244
245 Returns the current date and time, formatted for logging.
246
247 .. method:: address_string()
248
249 Returns the client address, formatted for logging. A name lookup is
250 performed on the client's IP address.
251
252
253.. class:: SimpleHTTPRequestHandler(request, client_address, server)
254
255 This class serves files from the current directory and below, directly
256 mapping the directory structure to HTTP requests.
257
258 A lot of the work, such as parsing the request, is done by the base class
259 :class:`BaseHTTPRequestHandler`. This class implements the :func:`do_GET`
260 and :func:`do_HEAD` functions.
261
262 The following are defined as class-level attributes of
263 :class:`SimpleHTTPRequestHandler`:
264
265 .. attribute:: server_version
266
267 This will be ``"SimpleHTTP/" + __version__``, where ``__version__`` is
268 defined at the module level.
269
270 .. attribute:: extensions_map
271
272 A dictionary mapping suffixes into MIME types. The default is
273 signified by an empty string, and is considered to be
274 ``application/octet-stream``. The mapping is used case-insensitively,
275 and so should contain only lower-cased keys.
276
277 The :class:`SimpleHTTPRequestHandler` class defines the following methods:
278
279 .. method:: do_HEAD()
280
281 This method serves the ``'HEAD'`` request type: it sends the headers it
282 would send for the equivalent ``GET`` request. See the :meth:`do_GET`
283 method for a more complete explanation of the possible headers.
284
285 .. method:: do_GET()
286
287 The request is mapped to a local file by interpreting the request as a
288 path relative to the current working directory.
289
290 If the request was mapped to a directory, the directory is checked for a
291 file named ``index.html`` or ``index.htm`` (in that order). If found, the
292 file's contents are returned; otherwise a directory listing is generated
293 by calling the :meth:`list_directory` method. This method uses
294 :func:`os.listdir` to scan the directory, and returns a ``404`` error
295 response if the :func:`listdir` fails.
296
297 If the request was mapped to a file, it is opened and the contents are
298 returned. Any :exc:`IOError` exception in opening the requested file is
299 mapped to a ``404``, ``'File not found'`` error. Otherwise, the content
300 type is guessed by calling the :meth:`guess_type` method, which in turn
301 uses the *extensions_map* variable.
302
303 A ``'Content-type:'`` header with the guessed content type is output,
304 followed by a ``'Content-Length:'`` header with the file's size and a
305 ``'Last-Modified:'`` header with the file's modification time.
306
307 Then follows a blank line signifying the end of the headers, and then the
308 contents of the file are output. If the file's MIME type starts with
309 ``text/`` the file is opened in text mode; otherwise binary mode is used.
310
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000311 For example usage, see the implementation of the :func:`test` function
312 invocation in the :mod:`http.server` module.
Georg Brandl24420152008-05-26 16:32:26 +0000313
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000314
Georg Brandl8971f742010-07-02 07:41:51 +0000315The :class:`SimpleHTTPRequestHandler` class can be used in the following
316manner in order to create a very basic webserver serving files relative to
317the current directory. ::
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000318
Georg Brandl8971f742010-07-02 07:41:51 +0000319 import http.server
320 import socketserver
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000321
Georg Brandl8971f742010-07-02 07:41:51 +0000322 PORT = 8000
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000323
Georg Brandl8971f742010-07-02 07:41:51 +0000324 Handler = http.server.SimpleHTTPRequestHandler
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000325
Georg Brandl8971f742010-07-02 07:41:51 +0000326 httpd = socketserver.TCPServer(("", PORT), Handler)
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000327
Georg Brandl8971f742010-07-02 07:41:51 +0000328 print("serving at port", PORT)
329 httpd.serve_forever()
330
Georg Brandlf68798b2010-07-03 10:22:10 +0000331:mod:`http.server` can also be invoked directly using the :option:`-m`
Georg Brandl8971f742010-07-02 07:41:51 +0000332switch of the interpreter a with ``port number`` argument. Similar to
333the previous example, this serves files relative to the current directory. ::
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000334
335 python -m http.server 8000
Georg Brandl24420152008-05-26 16:32:26 +0000336
Georg Brandl8971f742010-07-02 07:41:51 +0000337
Georg Brandl24420152008-05-26 16:32:26 +0000338.. class:: CGIHTTPRequestHandler(request, client_address, server)
339
340 This class is used to serve either files or output of CGI scripts from the
341 current directory and below. Note that mapping HTTP hierarchic structure to
342 local directory structure is exactly as in :class:`SimpleHTTPRequestHandler`.
343
344 .. note::
345
346 CGI scripts run by the :class:`CGIHTTPRequestHandler` class cannot execute
347 redirects (HTTP code 302), because code 200 (script output follows) is
348 sent prior to execution of the CGI script. This pre-empts the status
349 code.
350
351 The class will however, run the CGI script, instead of serving it as a file,
352 if it guesses it to be a CGI script. Only directory-based CGI are used ---
353 the other common server configuration is to treat special extensions as
354 denoting CGI scripts.
355
356 The :func:`do_GET` and :func:`do_HEAD` functions are modified to run CGI scripts
357 and serve the output, instead of serving files, if the request leads to
358 somewhere below the ``cgi_directories`` path.
359
360 The :class:`CGIHTTPRequestHandler` defines the following data member:
361
362 .. attribute:: cgi_directories
363
364 This defaults to ``['/cgi-bin', '/htbin']`` and describes directories to
365 treat as containing CGI scripts.
366
367 The :class:`CGIHTTPRequestHandler` defines the following method:
368
369 .. method:: do_POST()
370
371 This method serves the ``'POST'`` request type, only allowed for CGI
372 scripts. Error 501, "Can only POST to CGI scripts", is output when trying
373 to POST to a non-CGI url.
374
375 Note that CGI scripts will be run with UID of user nobody, for security
376 reasons. Problems with the CGI script will be translated to error 403.