blob: 7e317cd8bc2ba8dd75f281a6b7c0863a38b88b80 [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
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04007**Source code:** :source:`Lib/http/server.py`
Georg Brandl24420152008-05-26 16:32:26 +00008
9.. index::
10 pair: WWW; server
11 pair: HTTP; protocol
12 single: URL
13 single: httpd
14
Raymond Hettinger469271d2011-01-27 20:38:46 +000015--------------
16
Georg Brandl24420152008-05-26 16:32:26 +000017This module defines classes for implementing HTTP servers (Web servers).
18
Senthil Kumaranbb1876a2018-10-26 10:11:39 -070019.. warning::
20
21 :mod:`http.server` is not recommended for production. It only implements
22 basic security checks.
23
Georg Brandl24420152008-05-26 16:32:26 +000024One class, :class:`HTTPServer`, is a :class:`socketserver.TCPServer` subclass.
25It creates and listens at the HTTP socket, dispatching the requests to a
26handler. Code to create and run the server looks like this::
27
28 def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler):
29 server_address = ('', 8000)
30 httpd = server_class(server_address, handler_class)
31 httpd.serve_forever()
32
33
34.. class:: HTTPServer(server_address, RequestHandlerClass)
35
Serhiy Storchakabfdcd432013-10-13 23:09:14 +030036 This class builds on the :class:`~socketserver.TCPServer` class by storing
37 the server address as instance variables named :attr:`server_name` and
Georg Brandl24420152008-05-26 16:32:26 +000038 :attr:`server_port`. The server is accessible by the handler, typically
39 through the handler's :attr:`server` instance variable.
40
Julien Palard4f53e2a2018-05-30 02:24:17 +020041.. class:: ThreadingHTTPServer(server_address, RequestHandlerClass)
Georg Brandl24420152008-05-26 16:32:26 +000042
Miss Islington (bot)f8d2c3c2018-03-23 13:31:20 -070043 This class is identical to HTTPServer but uses threads to handle
Miss Islington (bot)252f10c2018-04-06 05:46:05 -070044 requests by using the :class:`~socketserver.ThreadingMixIn`. This
Miss Islington (bot)a48614b2018-03-29 01:33:47 -070045 is useful to handle web browsers pre-opening sockets, on which
46 :class:`HTTPServer` would wait indefinitely.
47
48 .. versionadded:: 3.7
49
Miss Islington (bot)f8d2c3c2018-03-23 13:31:20 -070050
Julien Palard4f53e2a2018-05-30 02:24:17 +020051The :class:`HTTPServer` and :class:`ThreadingHTTPServer` must be given
Miss Islington (bot)f8d2c3c2018-03-23 13:31:20 -070052a *RequestHandlerClass* on instantiation, of which this module
53provides three different variants:
Georg Brandl24420152008-05-26 16:32:26 +000054
55.. class:: BaseHTTPRequestHandler(request, client_address, server)
56
57 This class is used to handle the HTTP requests that arrive at the server. By
58 itself, it cannot respond to any actual HTTP requests; it must be subclassed
59 to handle each request method (e.g. GET or POST).
60 :class:`BaseHTTPRequestHandler` provides a number of class and instance
61 variables, and methods for use by subclasses.
62
63 The handler will parse the request and the headers, then call a method
64 specific to the request type. The method name is constructed from the
65 request. For example, for the request method ``SPAM``, the :meth:`do_SPAM`
66 method will be called with no arguments. All of the relevant information is
67 stored in instance variables of the handler. Subclasses should not need to
68 override or extend the :meth:`__init__` method.
69
70 :class:`BaseHTTPRequestHandler` has the following instance variables:
71
72 .. attribute:: client_address
73
74 Contains a tuple of the form ``(host, port)`` referring to the client's
75 address.
76
Benjamin Peterson3e4f0552008-09-02 00:31:15 +000077 .. attribute:: server
78
79 Contains the server instance.
80
Benjamin Peterson70e28472015-02-17 21:11:10 -050081 .. attribute:: close_connection
82
83 Boolean that should be set before :meth:`handle_one_request` returns,
84 indicating if another request may be expected, or if the connection should
85 be shut down.
86
87 .. attribute:: requestline
88
89 Contains the string representation of the HTTP request line. The
90 terminating CRLF is stripped. This attribute should be set by
91 :meth:`handle_one_request`. If no valid request line was processed, it
92 should be set to the empty string.
Benjamin Peterson3e4f0552008-09-02 00:31:15 +000093
Georg Brandl24420152008-05-26 16:32:26 +000094 .. attribute:: command
95
96 Contains the command (request type). For example, ``'GET'``.
97
98 .. attribute:: path
99
100 Contains the request path.
101
102 .. attribute:: request_version
103
104 Contains the version string from the request. For example, ``'HTTP/1.0'``.
105
106 .. attribute:: headers
107
108 Holds an instance of the class specified by the :attr:`MessageClass` class
109 variable. This instance parses and manages the headers in the HTTP
Senthil Kumarancad2bf22014-04-16 13:56:19 -0400110 request. The :func:`~http.client.parse_headers` function from
111 :mod:`http.client` is used to parse the headers and it requires that the
112 HTTP request provide a valid :rfc:`2822` style header.
113
Georg Brandl24420152008-05-26 16:32:26 +0000114 .. attribute:: rfile
115
Martin Panter34eeed42016-06-29 10:12:22 +0000116 An :class:`io.BufferedIOBase` input stream, ready to read from
117 the start of the optional input data.
Georg Brandl24420152008-05-26 16:32:26 +0000118
119 .. attribute:: wfile
120
121 Contains the output stream for writing a response back to the
122 client. Proper adherence to the HTTP protocol must be used when writing to
jugglinmikea083c8e2017-05-24 14:25:50 -0400123 this stream in order to achieve successful interoperation with HTTP
124 clients.
Georg Brandl24420152008-05-26 16:32:26 +0000125
Martin Panter34eeed42016-06-29 10:12:22 +0000126 .. versionchanged:: 3.6
127 This is an :class:`io.BufferedIOBase` stream.
128
Berker Peksag02698282016-04-24 01:51:02 +0300129 :class:`BaseHTTPRequestHandler` has the following attributes:
Georg Brandl24420152008-05-26 16:32:26 +0000130
131 .. attribute:: server_version
132
133 Specifies the server software version. You may want to override this. The
134 format is multiple whitespace-separated strings, where each string is of
135 the form name[/version]. For example, ``'BaseHTTP/0.2'``.
136
137 .. attribute:: sys_version
138
139 Contains the Python system version, in a form usable by the
140 :attr:`version_string` method and the :attr:`server_version` class
141 variable. For example, ``'Python/1.4'``.
142
143 .. attribute:: error_message_format
144
Berker Peksag02698282016-04-24 01:51:02 +0300145 Specifies a format string that should be used by :meth:`send_error` method
146 for building an error response to the client. The string is filled by
147 default with variables from :attr:`responses` based on the status code
148 that passed to :meth:`send_error`.
Georg Brandl24420152008-05-26 16:32:26 +0000149
150 .. attribute:: error_content_type
151
152 Specifies the Content-Type HTTP header of error responses sent to the
153 client. The default value is ``'text/html'``.
154
155 .. attribute:: protocol_version
156
157 This specifies the HTTP protocol version used in responses. If set to
158 ``'HTTP/1.1'``, the server will permit HTTP persistent connections;
159 however, your server *must* then include an accurate ``Content-Length``
160 header (using :meth:`send_header`) in all of its responses to clients.
161 For backwards compatibility, the setting defaults to ``'HTTP/1.0'``.
162
163 .. attribute:: MessageClass
164
Georg Brandl83e9f4c2008-06-12 18:52:31 +0000165 Specifies an :class:`email.message.Message`\ -like class to parse HTTP
166 headers. Typically, this is not overridden, and it defaults to
167 :class:`http.client.HTTPMessage`.
Georg Brandl24420152008-05-26 16:32:26 +0000168
169 .. attribute:: responses
170
Berker Peksag02698282016-04-24 01:51:02 +0300171 This attribute contains a mapping of error code integers to two-element tuples
Georg Brandl24420152008-05-26 16:32:26 +0000172 containing a short and long message. For example, ``{code: (shortmessage,
173 longmessage)}``. The *shortmessage* is usually used as the *message* key in an
Berker Peksag02698282016-04-24 01:51:02 +0300174 error response, and *longmessage* as the *explain* key. It is used by
175 :meth:`send_response_only` and :meth:`send_error` methods.
Georg Brandl24420152008-05-26 16:32:26 +0000176
177 A :class:`BaseHTTPRequestHandler` instance has the following methods:
178
179 .. method:: handle()
180
181 Calls :meth:`handle_one_request` once (or, if persistent connections are
182 enabled, multiple times) to handle incoming HTTP requests. You should
183 never need to override it; instead, implement appropriate :meth:`do_\*`
184 methods.
185
186 .. method:: handle_one_request()
187
188 This method will parse and dispatch the request to the appropriate
189 :meth:`do_\*` method. You should never need to override it.
190
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000191 .. method:: handle_expect_100()
192
Martin Panter7462b6492015-11-02 03:37:02 +0000193 When a HTTP/1.1 compliant server receives an ``Expect: 100-continue``
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000194 request header it responds back with a ``100 Continue`` followed by ``200
195 OK`` headers.
196 This method can be overridden to raise an error if the server does not
197 want the client to continue. For e.g. server can chose to send ``417
198 Expectation Failed`` as a response header and ``return False``.
199
200 .. versionadded:: 3.2
201
Senthil Kumaran26886442013-03-15 07:53:21 -0700202 .. method:: send_error(code, message=None, explain=None)
Georg Brandl24420152008-05-26 16:32:26 +0000203
204 Sends and logs a complete error reply to the client. The numeric *code*
R David Murraya475a8d2014-01-03 13:03:00 -0500205 specifies the HTTP error code, with *message* as an optional, short, human
206 readable description of the error. The *explain* argument can be used to
207 provide more detailed information about the error; it will be formatted
Berker Peksag02698282016-04-24 01:51:02 +0300208 using the :attr:`error_message_format` attribute and emitted, after
R David Murraya475a8d2014-01-03 13:03:00 -0500209 a complete set of headers, as the response body. The :attr:`responses`
Berker Peksag02698282016-04-24 01:51:02 +0300210 attribute holds the default values for *message* and *explain* that
R David Murraya475a8d2014-01-03 13:03:00 -0500211 will be used if no value is provided; for unknown codes the default value
Martin Pantere42e1292016-06-08 08:29:13 +0000212 for both is the string ``???``. The body will be empty if the method is
213 HEAD or the response code is one of the following: ``1xx``,
214 ``204 No Content``, ``205 Reset Content``, ``304 Not Modified``.
Georg Brandl24420152008-05-26 16:32:26 +0000215
Senthil Kumaran52d27202012-10-10 23:16:21 -0700216 .. versionchanged:: 3.4
217 The error response includes a Content-Length header.
Ezio Melotti2acd2932013-03-16 22:23:30 +0200218 Added the *explain* argument.
Senthil Kumaran26886442013-03-15 07:53:21 -0700219
Georg Brandl036490d2009-05-17 13:00:36 +0000220 .. method:: send_response(code, message=None)
Georg Brandl24420152008-05-26 16:32:26 +0000221
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800222 Adds a response header to the headers buffer and logs the accepted
Senthil Kumarancc995282011-05-11 16:04:28 +0800223 request. The HTTP response line is written to the internal buffer,
224 followed by *Server* and *Date* headers. The values for these two headers
225 are picked up from the :meth:`version_string` and
226 :meth:`date_time_string` methods, respectively. If the server does not
227 intend to send any other headers using the :meth:`send_header` method,
Martin Panter7462b6492015-11-02 03:37:02 +0000228 then :meth:`send_response` should be followed by an :meth:`end_headers`
Ezio Melottie9c7d6c2011-05-12 01:10:57 +0300229 call.
Senthil Kumarancc995282011-05-11 16:04:28 +0800230
Ezio Melottie9c7d6c2011-05-12 01:10:57 +0300231 .. versionchanged:: 3.3
232 Headers are stored to an internal buffer and :meth:`end_headers`
233 needs to be called explicitly.
Senthil Kumarancc995282011-05-11 16:04:28 +0800234
Georg Brandl24420152008-05-26 16:32:26 +0000235 .. method:: send_header(keyword, value)
236
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800237 Adds the HTTP header to an internal buffer which will be written to the
Senthil Kumaran6ea17a82011-05-11 11:45:48 +0800238 output stream when either :meth:`end_headers` or :meth:`flush_headers` is
239 invoked. *keyword* should specify the header keyword, with *value*
240 specifying its value. Note that, after the send_header calls are done,
241 :meth:`end_headers` MUST BE called in order to complete the operation.
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000242
Georg Brandl61063cc2012-06-24 22:48:30 +0200243 .. versionchanged:: 3.2
244 Headers are stored in an internal buffer.
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000245
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000246 .. method:: send_response_only(code, message=None)
247
Senthil Kumaranb4760ef2015-06-14 17:35:37 -0700248 Sends the response header only, used for the purposes when ``100
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000249 Continue`` response is sent by the server to the client. The headers not
250 buffered and sent directly the output stream.If the *message* is not
251 specified, the HTTP message corresponding the response *code* is sent.
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000252
253 .. versionadded:: 3.2
254
Georg Brandl24420152008-05-26 16:32:26 +0000255 .. method:: end_headers()
256
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800257 Adds a blank line
258 (indicating the end of the HTTP headers in the response)
Ezio Melottie9c7d6c2011-05-12 01:10:57 +0300259 to the headers buffer and calls :meth:`flush_headers()`.
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000260
Ezio Melottie9c7d6c2011-05-12 01:10:57 +0300261 .. versionchanged:: 3.2
262 The buffered headers are written to the output stream.
Georg Brandl24420152008-05-26 16:32:26 +0000263
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800264 .. method:: flush_headers()
265
266 Finally send the headers to the output stream and flush the internal
267 headers buffer.
268
269 .. versionadded:: 3.3
270
Georg Brandl036490d2009-05-17 13:00:36 +0000271 .. method:: log_request(code='-', size='-')
Georg Brandl24420152008-05-26 16:32:26 +0000272
273 Logs an accepted (successful) request. *code* should specify the numeric
274 HTTP code associated with the response. If a size of the response is
275 available, then it should be passed as the *size* parameter.
276
277 .. method:: log_error(...)
278
279 Logs an error when a request cannot be fulfilled. By default, it passes
280 the message to :meth:`log_message`, so it takes the same arguments
281 (*format* and additional values).
282
283
284 .. method:: log_message(format, ...)
285
286 Logs an arbitrary message to ``sys.stderr``. This is typically overridden
287 to create custom error logging mechanisms. The *format* argument is a
288 standard printf-style format string, where the additional arguments to
289 :meth:`log_message` are applied as inputs to the formatting. The client
Senthil Kumarandb727b42012-04-29 13:41:03 +0800290 ip address and current date and time are prefixed to every message logged.
Georg Brandl24420152008-05-26 16:32:26 +0000291
292 .. method:: version_string()
293
294 Returns the server software's version string. This is a combination of the
Berker Peksag02698282016-04-24 01:51:02 +0300295 :attr:`server_version` and :attr:`sys_version` attributes.
Georg Brandl24420152008-05-26 16:32:26 +0000296
Georg Brandl036490d2009-05-17 13:00:36 +0000297 .. method:: date_time_string(timestamp=None)
Georg Brandl24420152008-05-26 16:32:26 +0000298
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300299 Returns the date and time given by *timestamp* (which must be ``None`` or in
Georg Brandl036490d2009-05-17 13:00:36 +0000300 the format returned by :func:`time.time`), formatted for a message
301 header. If *timestamp* is omitted, it uses the current date and time.
Georg Brandl24420152008-05-26 16:32:26 +0000302
303 The result looks like ``'Sun, 06 Nov 1994 08:49:37 GMT'``.
304
305 .. method:: log_date_time_string()
306
307 Returns the current date and time, formatted for logging.
308
309 .. method:: address_string()
310
Senthil Kumaran1aacba42012-04-29 12:51:54 +0800311 Returns the client address.
312
313 .. versionchanged:: 3.3
314 Previously, a name lookup was performed. To avoid name resolution
315 delays, it now always returns the IP address.
Georg Brandl24420152008-05-26 16:32:26 +0000316
317
Stéphane Wirtela17a2f52017-05-24 09:29:06 +0200318.. class:: SimpleHTTPRequestHandler(request, client_address, server, directory=None)
Georg Brandl24420152008-05-26 16:32:26 +0000319
320 This class serves files from the current directory and below, directly
321 mapping the directory structure to HTTP requests.
322
323 A lot of the work, such as parsing the request, is done by the base class
324 :class:`BaseHTTPRequestHandler`. This class implements the :func:`do_GET`
325 and :func:`do_HEAD` functions.
326
327 The following are defined as class-level attributes of
328 :class:`SimpleHTTPRequestHandler`:
329
330 .. attribute:: server_version
331
332 This will be ``"SimpleHTTP/" + __version__``, where ``__version__`` is
333 defined at the module level.
334
335 .. attribute:: extensions_map
336
337 A dictionary mapping suffixes into MIME types. The default is
338 signified by an empty string, and is considered to be
339 ``application/octet-stream``. The mapping is used case-insensitively,
340 and so should contain only lower-cased keys.
341
Stéphane Wirtela17a2f52017-05-24 09:29:06 +0200342 .. attribute:: directory
343
344 If not specified, the directory to serve is the current working directory.
345
Georg Brandl24420152008-05-26 16:32:26 +0000346 The :class:`SimpleHTTPRequestHandler` class defines the following methods:
347
348 .. method:: do_HEAD()
349
350 This method serves the ``'HEAD'`` request type: it sends the headers it
351 would send for the equivalent ``GET`` request. See the :meth:`do_GET`
352 method for a more complete explanation of the possible headers.
353
354 .. method:: do_GET()
355
356 The request is mapped to a local file by interpreting the request as a
357 path relative to the current working directory.
358
359 If the request was mapped to a directory, the directory is checked for a
360 file named ``index.html`` or ``index.htm`` (in that order). If found, the
361 file's contents are returned; otherwise a directory listing is generated
362 by calling the :meth:`list_directory` method. This method uses
363 :func:`os.listdir` to scan the directory, and returns a ``404`` error
Serhiy Storchakabfdcd432013-10-13 23:09:14 +0300364 response if the :func:`~os.listdir` fails.
Georg Brandl24420152008-05-26 16:32:26 +0000365
Pierre Quentel351adda2017-04-02 12:26:12 +0200366 If the request was mapped to a file, it is opened. Any :exc:`OSError`
367 exception in opening the requested file is mapped to a ``404``,
368 ``'File not found'`` error. If there was a ``'If-Modified-Since'``
369 header in the request, and the file was not modified after this time,
370 a ``304``, ``'Not Modified'`` response is sent. Otherwise, the content
Georg Brandl24420152008-05-26 16:32:26 +0000371 type is guessed by calling the :meth:`guess_type` method, which in turn
Pierre Quentel351adda2017-04-02 12:26:12 +0200372 uses the *extensions_map* variable, and the file contents are returned.
Georg Brandl24420152008-05-26 16:32:26 +0000373
374 A ``'Content-type:'`` header with the guessed content type is output,
375 followed by a ``'Content-Length:'`` header with the file's size and a
376 ``'Last-Modified:'`` header with the file's modification time.
377
378 Then follows a blank line signifying the end of the headers, and then the
379 contents of the file are output. If the file's MIME type starts with
380 ``text/`` the file is opened in text mode; otherwise binary mode is used.
381
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000382 For example usage, see the implementation of the :func:`test` function
383 invocation in the :mod:`http.server` module.
Georg Brandl24420152008-05-26 16:32:26 +0000384
Pierre Quentel351adda2017-04-02 12:26:12 +0200385 .. versionchanged:: 3.7
386 Support of the ``'If-Modified-Since'`` header.
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000387
Georg Brandl8971f742010-07-02 07:41:51 +0000388The :class:`SimpleHTTPRequestHandler` class can be used in the following
389manner in order to create a very basic webserver serving files relative to
Larry Hastings3732ed22014-03-15 21:13:56 -0700390the current directory::
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000391
Georg Brandl8971f742010-07-02 07:41:51 +0000392 import http.server
393 import socketserver
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000394
Georg Brandl8971f742010-07-02 07:41:51 +0000395 PORT = 8000
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000396
Georg Brandl8971f742010-07-02 07:41:51 +0000397 Handler = http.server.SimpleHTTPRequestHandler
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000398
Martin Panter0cab9c12016-04-13 00:36:52 +0000399 with socketserver.TCPServer(("", PORT), Handler) as httpd:
400 print("serving at port", PORT)
401 httpd.serve_forever()
Georg Brandl8971f742010-07-02 07:41:51 +0000402
Larry Hastings3732ed22014-03-15 21:13:56 -0700403.. _http-server-cli:
404
Georg Brandlf68798b2010-07-03 10:22:10 +0000405:mod:`http.server` can also be invoked directly using the :option:`-m`
R David Murraye7bade52012-04-11 20:13:25 -0400406switch of the interpreter with a ``port number`` argument. Similar to
Larry Hastings3732ed22014-03-15 21:13:56 -0700407the previous example, this serves files relative to the current directory::
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000408
409 python -m http.server 8000
Georg Brandl24420152008-05-26 16:32:26 +0000410
Larry Hastings3732ed22014-03-15 21:13:56 -0700411By default, server binds itself to all interfaces. The option ``-b/--bind``
412specifies a specific address to which it should bind. For example, the
413following command causes the server to bind to localhost only::
Senthil Kumarandefe7f42013-09-15 09:37:27 -0700414
415 python -m http.server 8000 --bind 127.0.0.1
416
417.. versionadded:: 3.4
418 ``--bind`` argument was introduced.
419
Stéphane Wirtela17a2f52017-05-24 09:29:06 +0200420By default, server uses the current directory. The option ``-d/--directory``
421specifies a directory to which it should serve the files. For example,
422the following command uses a specific directory::
423
424 python -m http.server --directory /tmp/
425
426.. versionadded:: 3.7
427 ``--directory`` specify alternate directory
Georg Brandl8971f742010-07-02 07:41:51 +0000428
Georg Brandl24420152008-05-26 16:32:26 +0000429.. class:: CGIHTTPRequestHandler(request, client_address, server)
430
431 This class is used to serve either files or output of CGI scripts from the
432 current directory and below. Note that mapping HTTP hierarchic structure to
433 local directory structure is exactly as in :class:`SimpleHTTPRequestHandler`.
434
435 .. note::
436
437 CGI scripts run by the :class:`CGIHTTPRequestHandler` class cannot execute
438 redirects (HTTP code 302), because code 200 (script output follows) is
439 sent prior to execution of the CGI script. This pre-empts the status
440 code.
441
442 The class will however, run the CGI script, instead of serving it as a file,
443 if it guesses it to be a CGI script. Only directory-based CGI are used ---
444 the other common server configuration is to treat special extensions as
445 denoting CGI scripts.
446
447 The :func:`do_GET` and :func:`do_HEAD` functions are modified to run CGI scripts
448 and serve the output, instead of serving files, if the request leads to
449 somewhere below the ``cgi_directories`` path.
450
451 The :class:`CGIHTTPRequestHandler` defines the following data member:
452
453 .. attribute:: cgi_directories
454
455 This defaults to ``['/cgi-bin', '/htbin']`` and describes directories to
456 treat as containing CGI scripts.
457
458 The :class:`CGIHTTPRequestHandler` defines the following method:
459
460 .. method:: do_POST()
461
462 This method serves the ``'POST'`` request type, only allowed for CGI
463 scripts. Error 501, "Can only POST to CGI scripts", is output when trying
464 to POST to a non-CGI url.
465
466 Note that CGI scripts will be run with UID of user nobody, for security
467 reasons. Problems with the CGI script will be translated to error 403.
Senthil Kumaran1251faf2012-06-03 16:15:54 +0800468
469:class:`CGIHTTPRequestHandler` can be enabled in the command line by passing
Larry Hastings3732ed22014-03-15 21:13:56 -0700470the ``--cgi`` option::
Senthil Kumaran1251faf2012-06-03 16:15:54 +0800471
472 python -m http.server --cgi 8000