blob: 0b93c62288b189df9db0f48ce002d6adfee03504 [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
Felipe Rodrigues1d26c722018-10-10 23:43:40 -030019Security Considerations
20-----------------------
21
22http.server is meant for demo purposes and does not implement the stringent
23security checks needed of real HTTP server. We do not recommend
24using this module directly in production.
25
26
Georg Brandl24420152008-05-26 16:32:26 +000027One class, :class:`HTTPServer`, is a :class:`socketserver.TCPServer` subclass.
28It creates and listens at the HTTP socket, dispatching the requests to a
29handler. Code to create and run the server looks like this::
30
31 def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler):
32 server_address = ('', 8000)
33 httpd = server_class(server_address, handler_class)
34 httpd.serve_forever()
35
36
37.. class:: HTTPServer(server_address, RequestHandlerClass)
38
Serhiy Storchakabfdcd432013-10-13 23:09:14 +030039 This class builds on the :class:`~socketserver.TCPServer` class by storing
40 the server address as instance variables named :attr:`server_name` and
Georg Brandl24420152008-05-26 16:32:26 +000041 :attr:`server_port`. The server is accessible by the handler, typically
42 through the handler's :attr:`server` instance variable.
43
Géry Ogam1cee2162018-05-29 22:10:30 +020044.. class:: ThreadingHTTPServer(server_address, RequestHandlerClass)
Georg Brandl24420152008-05-26 16:32:26 +000045
Julien Palard8bcfa022018-03-23 17:40:33 +010046 This class is identical to HTTPServer but uses threads to handle
Alex Gaynor1d87c7b2018-04-06 08:26:49 -040047 requests by using the :class:`~socketserver.ThreadingMixIn`. This
Julien Palard79c3bab2018-03-28 23:24:58 +020048 is useful to handle web browsers pre-opening sockets, on which
49 :class:`HTTPServer` would wait indefinitely.
50
51 .. versionadded:: 3.7
52
Julien Palard8bcfa022018-03-23 17:40:33 +010053
Géry Ogam1cee2162018-05-29 22:10:30 +020054The :class:`HTTPServer` and :class:`ThreadingHTTPServer` must be given
Julien Palard8bcfa022018-03-23 17:40:33 +010055a *RequestHandlerClass* on instantiation, of which this module
56provides three different variants:
Georg Brandl24420152008-05-26 16:32:26 +000057
58.. class:: BaseHTTPRequestHandler(request, client_address, server)
59
60 This class is used to handle the HTTP requests that arrive at the server. By
61 itself, it cannot respond to any actual HTTP requests; it must be subclassed
62 to handle each request method (e.g. GET or POST).
63 :class:`BaseHTTPRequestHandler` provides a number of class and instance
64 variables, and methods for use by subclasses.
65
66 The handler will parse the request and the headers, then call a method
67 specific to the request type. The method name is constructed from the
68 request. For example, for the request method ``SPAM``, the :meth:`do_SPAM`
69 method will be called with no arguments. All of the relevant information is
70 stored in instance variables of the handler. Subclasses should not need to
71 override or extend the :meth:`__init__` method.
72
73 :class:`BaseHTTPRequestHandler` has the following instance variables:
74
75 .. attribute:: client_address
76
77 Contains a tuple of the form ``(host, port)`` referring to the client's
78 address.
79
Benjamin Peterson3e4f0552008-09-02 00:31:15 +000080 .. attribute:: server
81
82 Contains the server instance.
83
Benjamin Peterson70e28472015-02-17 21:11:10 -050084 .. attribute:: close_connection
85
86 Boolean that should be set before :meth:`handle_one_request` returns,
87 indicating if another request may be expected, or if the connection should
88 be shut down.
89
90 .. attribute:: requestline
91
92 Contains the string representation of the HTTP request line. The
93 terminating CRLF is stripped. This attribute should be set by
94 :meth:`handle_one_request`. If no valid request line was processed, it
95 should be set to the empty string.
Benjamin Peterson3e4f0552008-09-02 00:31:15 +000096
Georg Brandl24420152008-05-26 16:32:26 +000097 .. attribute:: command
98
99 Contains the command (request type). For example, ``'GET'``.
100
101 .. attribute:: path
102
103 Contains the request path.
104
105 .. attribute:: request_version
106
107 Contains the version string from the request. For example, ``'HTTP/1.0'``.
108
109 .. attribute:: headers
110
111 Holds an instance of the class specified by the :attr:`MessageClass` class
112 variable. This instance parses and manages the headers in the HTTP
Senthil Kumarancad2bf22014-04-16 13:56:19 -0400113 request. The :func:`~http.client.parse_headers` function from
114 :mod:`http.client` is used to parse the headers and it requires that the
115 HTTP request provide a valid :rfc:`2822` style header.
116
Georg Brandl24420152008-05-26 16:32:26 +0000117 .. attribute:: rfile
118
Martin Panter34eeed42016-06-29 10:12:22 +0000119 An :class:`io.BufferedIOBase` input stream, ready to read from
120 the start of the optional input data.
Georg Brandl24420152008-05-26 16:32:26 +0000121
122 .. attribute:: wfile
123
124 Contains the output stream for writing a response back to the
125 client. Proper adherence to the HTTP protocol must be used when writing to
jugglinmikea083c8e2017-05-24 14:25:50 -0400126 this stream in order to achieve successful interoperation with HTTP
127 clients.
Georg Brandl24420152008-05-26 16:32:26 +0000128
Martin Panter34eeed42016-06-29 10:12:22 +0000129 .. versionchanged:: 3.6
130 This is an :class:`io.BufferedIOBase` stream.
131
Berker Peksag02698282016-04-24 01:51:02 +0300132 :class:`BaseHTTPRequestHandler` has the following attributes:
Georg Brandl24420152008-05-26 16:32:26 +0000133
134 .. attribute:: server_version
135
136 Specifies the server software version. You may want to override this. The
137 format is multiple whitespace-separated strings, where each string is of
138 the form name[/version]. For example, ``'BaseHTTP/0.2'``.
139
140 .. attribute:: sys_version
141
142 Contains the Python system version, in a form usable by the
143 :attr:`version_string` method and the :attr:`server_version` class
144 variable. For example, ``'Python/1.4'``.
145
146 .. attribute:: error_message_format
147
Berker Peksag02698282016-04-24 01:51:02 +0300148 Specifies a format string that should be used by :meth:`send_error` method
149 for building an error response to the client. The string is filled by
150 default with variables from :attr:`responses` based on the status code
151 that passed to :meth:`send_error`.
Georg Brandl24420152008-05-26 16:32:26 +0000152
153 .. attribute:: error_content_type
154
155 Specifies the Content-Type HTTP header of error responses sent to the
156 client. The default value is ``'text/html'``.
157
158 .. attribute:: protocol_version
159
160 This specifies the HTTP protocol version used in responses. If set to
161 ``'HTTP/1.1'``, the server will permit HTTP persistent connections;
162 however, your server *must* then include an accurate ``Content-Length``
163 header (using :meth:`send_header`) in all of its responses to clients.
164 For backwards compatibility, the setting defaults to ``'HTTP/1.0'``.
165
166 .. attribute:: MessageClass
167
Georg Brandl83e9f4c2008-06-12 18:52:31 +0000168 Specifies an :class:`email.message.Message`\ -like class to parse HTTP
169 headers. Typically, this is not overridden, and it defaults to
170 :class:`http.client.HTTPMessage`.
Georg Brandl24420152008-05-26 16:32:26 +0000171
172 .. attribute:: responses
173
Berker Peksag02698282016-04-24 01:51:02 +0300174 This attribute contains a mapping of error code integers to two-element tuples
Georg Brandl24420152008-05-26 16:32:26 +0000175 containing a short and long message. For example, ``{code: (shortmessage,
176 longmessage)}``. The *shortmessage* is usually used as the *message* key in an
Berker Peksag02698282016-04-24 01:51:02 +0300177 error response, and *longmessage* as the *explain* key. It is used by
178 :meth:`send_response_only` and :meth:`send_error` methods.
Georg Brandl24420152008-05-26 16:32:26 +0000179
180 A :class:`BaseHTTPRequestHandler` instance has the following methods:
181
182 .. method:: handle()
183
184 Calls :meth:`handle_one_request` once (or, if persistent connections are
185 enabled, multiple times) to handle incoming HTTP requests. You should
186 never need to override it; instead, implement appropriate :meth:`do_\*`
187 methods.
188
189 .. method:: handle_one_request()
190
191 This method will parse and dispatch the request to the appropriate
192 :meth:`do_\*` method. You should never need to override it.
193
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000194 .. method:: handle_expect_100()
195
Martin Panter7462b6492015-11-02 03:37:02 +0000196 When a HTTP/1.1 compliant server receives an ``Expect: 100-continue``
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000197 request header it responds back with a ``100 Continue`` followed by ``200
198 OK`` headers.
199 This method can be overridden to raise an error if the server does not
200 want the client to continue. For e.g. server can chose to send ``417
201 Expectation Failed`` as a response header and ``return False``.
202
203 .. versionadded:: 3.2
204
Senthil Kumaran26886442013-03-15 07:53:21 -0700205 .. method:: send_error(code, message=None, explain=None)
Georg Brandl24420152008-05-26 16:32:26 +0000206
207 Sends and logs a complete error reply to the client. The numeric *code*
R David Murraya475a8d2014-01-03 13:03:00 -0500208 specifies the HTTP error code, with *message* as an optional, short, human
209 readable description of the error. The *explain* argument can be used to
210 provide more detailed information about the error; it will be formatted
Berker Peksag02698282016-04-24 01:51:02 +0300211 using the :attr:`error_message_format` attribute and emitted, after
R David Murraya475a8d2014-01-03 13:03:00 -0500212 a complete set of headers, as the response body. The :attr:`responses`
Berker Peksag02698282016-04-24 01:51:02 +0300213 attribute holds the default values for *message* and *explain* that
R David Murraya475a8d2014-01-03 13:03:00 -0500214 will be used if no value is provided; for unknown codes the default value
Martin Pantere42e1292016-06-08 08:29:13 +0000215 for both is the string ``???``. The body will be empty if the method is
216 HEAD or the response code is one of the following: ``1xx``,
217 ``204 No Content``, ``205 Reset Content``, ``304 Not Modified``.
Georg Brandl24420152008-05-26 16:32:26 +0000218
Senthil Kumaran52d27202012-10-10 23:16:21 -0700219 .. versionchanged:: 3.4
220 The error response includes a Content-Length header.
Ezio Melotti2acd2932013-03-16 22:23:30 +0200221 Added the *explain* argument.
Senthil Kumaran26886442013-03-15 07:53:21 -0700222
Georg Brandl036490d2009-05-17 13:00:36 +0000223 .. method:: send_response(code, message=None)
Georg Brandl24420152008-05-26 16:32:26 +0000224
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800225 Adds a response header to the headers buffer and logs the accepted
Senthil Kumarancc995282011-05-11 16:04:28 +0800226 request. The HTTP response line is written to the internal buffer,
227 followed by *Server* and *Date* headers. The values for these two headers
228 are picked up from the :meth:`version_string` and
229 :meth:`date_time_string` methods, respectively. If the server does not
230 intend to send any other headers using the :meth:`send_header` method,
Martin Panter7462b6492015-11-02 03:37:02 +0000231 then :meth:`send_response` should be followed by an :meth:`end_headers`
Ezio Melottie9c7d6c2011-05-12 01:10:57 +0300232 call.
Senthil Kumarancc995282011-05-11 16:04:28 +0800233
Ezio Melottie9c7d6c2011-05-12 01:10:57 +0300234 .. versionchanged:: 3.3
235 Headers are stored to an internal buffer and :meth:`end_headers`
236 needs to be called explicitly.
Senthil Kumarancc995282011-05-11 16:04:28 +0800237
Georg Brandl24420152008-05-26 16:32:26 +0000238 .. method:: send_header(keyword, value)
239
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800240 Adds the HTTP header to an internal buffer which will be written to the
Senthil Kumaran6ea17a82011-05-11 11:45:48 +0800241 output stream when either :meth:`end_headers` or :meth:`flush_headers` is
242 invoked. *keyword* should specify the header keyword, with *value*
243 specifying its value. Note that, after the send_header calls are done,
244 :meth:`end_headers` MUST BE called in order to complete the operation.
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000245
Georg Brandl61063cc2012-06-24 22:48:30 +0200246 .. versionchanged:: 3.2
247 Headers are stored in an internal buffer.
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000248
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000249 .. method:: send_response_only(code, message=None)
250
Senthil Kumaranb4760ef2015-06-14 17:35:37 -0700251 Sends the response header only, used for the purposes when ``100
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000252 Continue`` response is sent by the server to the client. The headers not
253 buffered and sent directly the output stream.If the *message* is not
254 specified, the HTTP message corresponding the response *code* is sent.
Senthil Kumaran0f476d42010-09-30 06:09:18 +0000255
256 .. versionadded:: 3.2
257
Georg Brandl24420152008-05-26 16:32:26 +0000258 .. method:: end_headers()
259
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800260 Adds a blank line
261 (indicating the end of the HTTP headers in the response)
Ezio Melottie9c7d6c2011-05-12 01:10:57 +0300262 to the headers buffer and calls :meth:`flush_headers()`.
Senthil Kumarane4dad4f2010-11-21 14:36:14 +0000263
Ezio Melottie9c7d6c2011-05-12 01:10:57 +0300264 .. versionchanged:: 3.2
265 The buffered headers are written to the output stream.
Georg Brandl24420152008-05-26 16:32:26 +0000266
Senthil Kumaranc7ae19b2011-05-09 23:25:02 +0800267 .. method:: flush_headers()
268
269 Finally send the headers to the output stream and flush the internal
270 headers buffer.
271
272 .. versionadded:: 3.3
273
Georg Brandl036490d2009-05-17 13:00:36 +0000274 .. method:: log_request(code='-', size='-')
Georg Brandl24420152008-05-26 16:32:26 +0000275
276 Logs an accepted (successful) request. *code* should specify the numeric
277 HTTP code associated with the response. If a size of the response is
278 available, then it should be passed as the *size* parameter.
279
280 .. method:: log_error(...)
281
282 Logs an error when a request cannot be fulfilled. By default, it passes
283 the message to :meth:`log_message`, so it takes the same arguments
284 (*format* and additional values).
285
286
287 .. method:: log_message(format, ...)
288
289 Logs an arbitrary message to ``sys.stderr``. This is typically overridden
290 to create custom error logging mechanisms. The *format* argument is a
291 standard printf-style format string, where the additional arguments to
292 :meth:`log_message` are applied as inputs to the formatting. The client
Senthil Kumarandb727b42012-04-29 13:41:03 +0800293 ip address and current date and time are prefixed to every message logged.
Georg Brandl24420152008-05-26 16:32:26 +0000294
295 .. method:: version_string()
296
297 Returns the server software's version string. This is a combination of the
Berker Peksag02698282016-04-24 01:51:02 +0300298 :attr:`server_version` and :attr:`sys_version` attributes.
Georg Brandl24420152008-05-26 16:32:26 +0000299
Georg Brandl036490d2009-05-17 13:00:36 +0000300 .. method:: date_time_string(timestamp=None)
Georg Brandl24420152008-05-26 16:32:26 +0000301
Serhiy Storchakaecf41da2016-10-19 16:29:26 +0300302 Returns the date and time given by *timestamp* (which must be ``None`` or in
Georg Brandl036490d2009-05-17 13:00:36 +0000303 the format returned by :func:`time.time`), formatted for a message
304 header. If *timestamp* is omitted, it uses the current date and time.
Georg Brandl24420152008-05-26 16:32:26 +0000305
306 The result looks like ``'Sun, 06 Nov 1994 08:49:37 GMT'``.
307
308 .. method:: log_date_time_string()
309
310 Returns the current date and time, formatted for logging.
311
312 .. method:: address_string()
313
Senthil Kumaran1aacba42012-04-29 12:51:54 +0800314 Returns the client address.
315
316 .. versionchanged:: 3.3
317 Previously, a name lookup was performed. To avoid name resolution
318 delays, it now always returns the IP address.
Georg Brandl24420152008-05-26 16:32:26 +0000319
320
Stéphane Wirtela17a2f52017-05-24 09:29:06 +0200321.. class:: SimpleHTTPRequestHandler(request, client_address, server, directory=None)
Georg Brandl24420152008-05-26 16:32:26 +0000322
323 This class serves files from the current directory and below, directly
324 mapping the directory structure to HTTP requests.
325
326 A lot of the work, such as parsing the request, is done by the base class
327 :class:`BaseHTTPRequestHandler`. This class implements the :func:`do_GET`
328 and :func:`do_HEAD` functions.
329
330 The following are defined as class-level attributes of
331 :class:`SimpleHTTPRequestHandler`:
332
333 .. attribute:: server_version
334
335 This will be ``"SimpleHTTP/" + __version__``, where ``__version__`` is
336 defined at the module level.
337
338 .. attribute:: extensions_map
339
340 A dictionary mapping suffixes into MIME types. The default is
341 signified by an empty string, and is considered to be
342 ``application/octet-stream``. The mapping is used case-insensitively,
343 and so should contain only lower-cased keys.
344
Stéphane Wirtela17a2f52017-05-24 09:29:06 +0200345 .. attribute:: directory
346
347 If not specified, the directory to serve is the current working directory.
348
Georg Brandl24420152008-05-26 16:32:26 +0000349 The :class:`SimpleHTTPRequestHandler` class defines the following methods:
350
351 .. method:: do_HEAD()
352
353 This method serves the ``'HEAD'`` request type: it sends the headers it
354 would send for the equivalent ``GET`` request. See the :meth:`do_GET`
355 method for a more complete explanation of the possible headers.
356
357 .. method:: do_GET()
358
359 The request is mapped to a local file by interpreting the request as a
360 path relative to the current working directory.
361
362 If the request was mapped to a directory, the directory is checked for a
363 file named ``index.html`` or ``index.htm`` (in that order). If found, the
364 file's contents are returned; otherwise a directory listing is generated
365 by calling the :meth:`list_directory` method. This method uses
366 :func:`os.listdir` to scan the directory, and returns a ``404`` error
Serhiy Storchakabfdcd432013-10-13 23:09:14 +0300367 response if the :func:`~os.listdir` fails.
Georg Brandl24420152008-05-26 16:32:26 +0000368
Pierre Quentel351adda2017-04-02 12:26:12 +0200369 If the request was mapped to a file, it is opened. Any :exc:`OSError`
370 exception in opening the requested file is mapped to a ``404``,
371 ``'File not found'`` error. If there was a ``'If-Modified-Since'``
372 header in the request, and the file was not modified after this time,
373 a ``304``, ``'Not Modified'`` response is sent. Otherwise, the content
Georg Brandl24420152008-05-26 16:32:26 +0000374 type is guessed by calling the :meth:`guess_type` method, which in turn
Pierre Quentel351adda2017-04-02 12:26:12 +0200375 uses the *extensions_map* variable, and the file contents are returned.
Georg Brandl24420152008-05-26 16:32:26 +0000376
377 A ``'Content-type:'`` header with the guessed content type is output,
378 followed by a ``'Content-Length:'`` header with the file's size and a
379 ``'Last-Modified:'`` header with the file's modification time.
380
381 Then follows a blank line signifying the end of the headers, and then the
382 contents of the file are output. If the file's MIME type starts with
383 ``text/`` the file is opened in text mode; otherwise binary mode is used.
384
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000385 For example usage, see the implementation of the :func:`test` function
386 invocation in the :mod:`http.server` module.
Georg Brandl24420152008-05-26 16:32:26 +0000387
Pierre Quentel351adda2017-04-02 12:26:12 +0200388 .. versionchanged:: 3.7
389 Support of the ``'If-Modified-Since'`` header.
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000390
Georg Brandl8971f742010-07-02 07:41:51 +0000391The :class:`SimpleHTTPRequestHandler` class can be used in the following
392manner in order to create a very basic webserver serving files relative to
Larry Hastings3732ed22014-03-15 21:13:56 -0700393the current directory::
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000394
Georg Brandl8971f742010-07-02 07:41:51 +0000395 import http.server
396 import socketserver
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000397
Georg Brandl8971f742010-07-02 07:41:51 +0000398 PORT = 8000
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000399
Georg Brandl8971f742010-07-02 07:41:51 +0000400 Handler = http.server.SimpleHTTPRequestHandler
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000401
Martin Panter0cab9c12016-04-13 00:36:52 +0000402 with socketserver.TCPServer(("", PORT), Handler) as httpd:
403 print("serving at port", PORT)
404 httpd.serve_forever()
Georg Brandl8971f742010-07-02 07:41:51 +0000405
Larry Hastings3732ed22014-03-15 21:13:56 -0700406.. _http-server-cli:
407
Georg Brandlf68798b2010-07-03 10:22:10 +0000408:mod:`http.server` can also be invoked directly using the :option:`-m`
R David Murraye7bade52012-04-11 20:13:25 -0400409switch of the interpreter with a ``port number`` argument. Similar to
Larry Hastings3732ed22014-03-15 21:13:56 -0700410the previous example, this serves files relative to the current directory::
Senthil Kumaran97db43b2010-06-16 16:41:11 +0000411
412 python -m http.server 8000
Georg Brandl24420152008-05-26 16:32:26 +0000413
Larry Hastings3732ed22014-03-15 21:13:56 -0700414By default, server binds itself to all interfaces. The option ``-b/--bind``
415specifies a specific address to which it should bind. For example, the
416following command causes the server to bind to localhost only::
Senthil Kumarandefe7f42013-09-15 09:37:27 -0700417
418 python -m http.server 8000 --bind 127.0.0.1
419
420.. versionadded:: 3.4
421 ``--bind`` argument was introduced.
422
Stéphane Wirtela17a2f52017-05-24 09:29:06 +0200423By default, server uses the current directory. The option ``-d/--directory``
424specifies a directory to which it should serve the files. For example,
425the following command uses a specific directory::
426
427 python -m http.server --directory /tmp/
428
429.. versionadded:: 3.7
430 ``--directory`` specify alternate directory
Georg Brandl8971f742010-07-02 07:41:51 +0000431
Georg Brandl24420152008-05-26 16:32:26 +0000432.. class:: CGIHTTPRequestHandler(request, client_address, server)
433
434 This class is used to serve either files or output of CGI scripts from the
435 current directory and below. Note that mapping HTTP hierarchic structure to
436 local directory structure is exactly as in :class:`SimpleHTTPRequestHandler`.
437
438 .. note::
439
440 CGI scripts run by the :class:`CGIHTTPRequestHandler` class cannot execute
441 redirects (HTTP code 302), because code 200 (script output follows) is
442 sent prior to execution of the CGI script. This pre-empts the status
443 code.
444
445 The class will however, run the CGI script, instead of serving it as a file,
446 if it guesses it to be a CGI script. Only directory-based CGI are used ---
447 the other common server configuration is to treat special extensions as
448 denoting CGI scripts.
449
450 The :func:`do_GET` and :func:`do_HEAD` functions are modified to run CGI scripts
451 and serve the output, instead of serving files, if the request leads to
452 somewhere below the ``cgi_directories`` path.
453
454 The :class:`CGIHTTPRequestHandler` defines the following data member:
455
456 .. attribute:: cgi_directories
457
458 This defaults to ``['/cgi-bin', '/htbin']`` and describes directories to
459 treat as containing CGI scripts.
460
461 The :class:`CGIHTTPRequestHandler` defines the following method:
462
463 .. method:: do_POST()
464
465 This method serves the ``'POST'`` request type, only allowed for CGI
466 scripts. Error 501, "Can only POST to CGI scripts", is output when trying
467 to POST to a non-CGI url.
468
469 Note that CGI scripts will be run with UID of user nobody, for security
470 reasons. Problems with the CGI script will be translated to error 403.
Senthil Kumaran1251faf2012-06-03 16:15:54 +0800471
472:class:`CGIHTTPRequestHandler` can be enabled in the command line by passing
Larry Hastings3732ed22014-03-15 21:13:56 -0700473the ``--cgi`` option::
Senthil Kumaran1251faf2012-06-03 16:15:54 +0800474
475 python -m http.server --cgi 8000