Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 1 | :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 | |
Raymond Hettinger | 469271d | 2011-01-27 20:38:46 +0000 | [diff] [blame] | 14 | **Source code:** :source:`Lib/http/server.py` |
| 15 | |
| 16 | -------------- |
| 17 | |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 18 | This module defines classes for implementing HTTP servers (Web servers). |
| 19 | |
| 20 | One class, :class:`HTTPServer`, is a :class:`socketserver.TCPServer` subclass. |
| 21 | It creates and listens at the HTTP socket, dispatching the requests to a |
| 22 | handler. Code to create and run the server looks like this:: |
| 23 | |
| 24 | def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler): |
| 25 | server_address = ('', 8000) |
| 26 | httpd = server_class(server_address, handler_class) |
| 27 | httpd.serve_forever() |
| 28 | |
| 29 | |
| 30 | .. class:: HTTPServer(server_address, RequestHandlerClass) |
| 31 | |
| 32 | This class builds on the :class:`TCPServer` class by storing the server |
| 33 | address as instance variables named :attr:`server_name` and |
| 34 | :attr:`server_port`. The server is accessible by the handler, typically |
| 35 | through the handler's :attr:`server` instance variable. |
| 36 | |
| 37 | |
| 38 | The :class:`HTTPServer` must be given a *RequestHandlerClass* on instantiation, |
| 39 | of which this module provides three different variants: |
| 40 | |
| 41 | .. class:: BaseHTTPRequestHandler(request, client_address, server) |
| 42 | |
| 43 | This class is used to handle the HTTP requests that arrive at the server. By |
| 44 | itself, it cannot respond to any actual HTTP requests; it must be subclassed |
| 45 | to handle each request method (e.g. GET or POST). |
| 46 | :class:`BaseHTTPRequestHandler` provides a number of class and instance |
| 47 | variables, and methods for use by subclasses. |
| 48 | |
| 49 | The handler will parse the request and the headers, then call a method |
| 50 | specific to the request type. The method name is constructed from the |
| 51 | request. For example, for the request method ``SPAM``, the :meth:`do_SPAM` |
| 52 | method will be called with no arguments. All of the relevant information is |
| 53 | stored in instance variables of the handler. Subclasses should not need to |
| 54 | override or extend the :meth:`__init__` method. |
| 55 | |
| 56 | :class:`BaseHTTPRequestHandler` has the following instance variables: |
| 57 | |
| 58 | .. attribute:: client_address |
| 59 | |
| 60 | Contains a tuple of the form ``(host, port)`` referring to the client's |
| 61 | address. |
| 62 | |
Benjamin Peterson | 3e4f055 | 2008-09-02 00:31:15 +0000 | [diff] [blame] | 63 | .. attribute:: server |
| 64 | |
| 65 | Contains the server instance. |
| 66 | |
| 67 | |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 68 | .. attribute:: command |
| 69 | |
| 70 | Contains the command (request type). For example, ``'GET'``. |
| 71 | |
| 72 | .. attribute:: path |
| 73 | |
| 74 | Contains the request path. |
| 75 | |
| 76 | .. attribute:: request_version |
| 77 | |
| 78 | Contains the version string from the request. For example, ``'HTTP/1.0'``. |
| 79 | |
| 80 | .. attribute:: headers |
| 81 | |
| 82 | Holds an instance of the class specified by the :attr:`MessageClass` class |
| 83 | variable. This instance parses and manages the headers in the HTTP |
| 84 | request. |
| 85 | |
| 86 | .. attribute:: rfile |
| 87 | |
| 88 | Contains an input stream, positioned at the start of the optional input |
| 89 | data. |
| 90 | |
| 91 | .. attribute:: wfile |
| 92 | |
| 93 | Contains the output stream for writing a response back to the |
| 94 | client. Proper adherence to the HTTP protocol must be used when writing to |
| 95 | this stream. |
| 96 | |
| 97 | :class:`BaseHTTPRequestHandler` has the following class variables: |
| 98 | |
| 99 | .. attribute:: server_version |
| 100 | |
| 101 | Specifies the server software version. You may want to override this. The |
| 102 | format is multiple whitespace-separated strings, where each string is of |
| 103 | the form name[/version]. For example, ``'BaseHTTP/0.2'``. |
| 104 | |
| 105 | .. attribute:: sys_version |
| 106 | |
| 107 | Contains the Python system version, in a form usable by the |
| 108 | :attr:`version_string` method and the :attr:`server_version` class |
| 109 | variable. For example, ``'Python/1.4'``. |
| 110 | |
| 111 | .. attribute:: error_message_format |
| 112 | |
| 113 | Specifies a format string for building an error response to the client. It |
| 114 | uses parenthesized, keyed format specifiers, so the format operand must be |
| 115 | a dictionary. The *code* key should be an integer, specifying the numeric |
| 116 | HTTP error code value. *message* should be a string containing a |
| 117 | (detailed) error message of what occurred, and *explain* should be an |
| 118 | explanation of the error code number. Default *message* and *explain* |
| 119 | values can found in the *responses* class variable. |
| 120 | |
| 121 | .. attribute:: error_content_type |
| 122 | |
| 123 | Specifies the Content-Type HTTP header of error responses sent to the |
| 124 | client. The default value is ``'text/html'``. |
| 125 | |
| 126 | .. attribute:: protocol_version |
| 127 | |
| 128 | This specifies the HTTP protocol version used in responses. If set to |
| 129 | ``'HTTP/1.1'``, the server will permit HTTP persistent connections; |
| 130 | however, your server *must* then include an accurate ``Content-Length`` |
| 131 | header (using :meth:`send_header`) in all of its responses to clients. |
| 132 | For backwards compatibility, the setting defaults to ``'HTTP/1.0'``. |
| 133 | |
| 134 | .. attribute:: MessageClass |
| 135 | |
Georg Brandl | 83e9f4c | 2008-06-12 18:52:31 +0000 | [diff] [blame] | 136 | Specifies an :class:`email.message.Message`\ -like class to parse HTTP |
| 137 | headers. Typically, this is not overridden, and it defaults to |
| 138 | :class:`http.client.HTTPMessage`. |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 139 | |
| 140 | .. attribute:: responses |
| 141 | |
| 142 | This variable contains a mapping of error code integers to two-element tuples |
| 143 | containing a short and long message. For example, ``{code: (shortmessage, |
| 144 | longmessage)}``. The *shortmessage* is usually used as the *message* key in an |
| 145 | error response, and *longmessage* as the *explain* key (see the |
| 146 | :attr:`error_message_format` class variable). |
| 147 | |
| 148 | A :class:`BaseHTTPRequestHandler` instance has the following methods: |
| 149 | |
| 150 | .. method:: handle() |
| 151 | |
| 152 | Calls :meth:`handle_one_request` once (or, if persistent connections are |
| 153 | enabled, multiple times) to handle incoming HTTP requests. You should |
| 154 | never need to override it; instead, implement appropriate :meth:`do_\*` |
| 155 | methods. |
| 156 | |
| 157 | .. method:: handle_one_request() |
| 158 | |
| 159 | This method will parse and dispatch the request to the appropriate |
| 160 | :meth:`do_\*` method. You should never need to override it. |
| 161 | |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 162 | .. method:: handle_expect_100() |
| 163 | |
| 164 | When a HTTP/1.1 compliant server receives a ``Expect: 100-continue`` |
| 165 | request header it responds back with a ``100 Continue`` followed by ``200 |
| 166 | OK`` headers. |
| 167 | This method can be overridden to raise an error if the server does not |
| 168 | want the client to continue. For e.g. server can chose to send ``417 |
| 169 | Expectation Failed`` as a response header and ``return False``. |
| 170 | |
| 171 | .. versionadded:: 3.2 |
| 172 | |
Georg Brandl | 036490d | 2009-05-17 13:00:36 +0000 | [diff] [blame] | 173 | .. method:: send_error(code, message=None) |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 174 | |
| 175 | Sends and logs a complete error reply to the client. The numeric *code* |
| 176 | specifies the HTTP error code, with *message* as optional, more specific text. A |
| 177 | complete set of headers is sent, followed by text composed using the |
| 178 | :attr:`error_message_format` class variable. |
| 179 | |
Georg Brandl | 036490d | 2009-05-17 13:00:36 +0000 | [diff] [blame] | 180 | .. method:: send_response(code, message=None) |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 181 | |
Senthil Kumaran | c7ae19b | 2011-05-09 23:25:02 +0800 | [diff] [blame] | 182 | Adds a response header to the headers buffer and logs the accepted |
Senthil Kumaran | cc99528 | 2011-05-11 16:04:28 +0800 | [diff] [blame] | 183 | request. The HTTP response line is written to the internal buffer, |
| 184 | followed by *Server* and *Date* headers. The values for these two headers |
| 185 | are picked up from the :meth:`version_string` and |
| 186 | :meth:`date_time_string` methods, respectively. If the server does not |
| 187 | intend to send any other headers using the :meth:`send_header` method, |
Ezio Melotti | e9c7d6c | 2011-05-12 01:10:57 +0300 | [diff] [blame] | 188 | then :meth:`send_response` should be followed by a :meth:`end_headers` |
| 189 | call. |
Senthil Kumaran | cc99528 | 2011-05-11 16:04:28 +0800 | [diff] [blame] | 190 | |
Ezio Melotti | e9c7d6c | 2011-05-12 01:10:57 +0300 | [diff] [blame] | 191 | .. versionchanged:: 3.3 |
| 192 | Headers are stored to an internal buffer and :meth:`end_headers` |
| 193 | needs to be called explicitly. |
Senthil Kumaran | cc99528 | 2011-05-11 16:04:28 +0800 | [diff] [blame] | 194 | |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 195 | |
| 196 | .. method:: send_header(keyword, value) |
| 197 | |
Senthil Kumaran | c7ae19b | 2011-05-09 23:25:02 +0800 | [diff] [blame] | 198 | Adds the HTTP header to an internal buffer which will be written to the |
Senthil Kumaran | 6ea17a8 | 2011-05-11 11:45:48 +0800 | [diff] [blame] | 199 | output stream when either :meth:`end_headers` or :meth:`flush_headers` is |
| 200 | invoked. *keyword* should specify the header keyword, with *value* |
| 201 | specifying its value. Note that, after the send_header calls are done, |
| 202 | :meth:`end_headers` MUST BE called in order to complete the operation. |
Senthil Kumaran | e4dad4f | 2010-11-21 14:36:14 +0000 | [diff] [blame] | 203 | |
Georg Brandl | 61063cc | 2012-06-24 22:48:30 +0200 | [diff] [blame] | 204 | .. versionchanged:: 3.2 |
| 205 | Headers are stored in an internal buffer. |
Senthil Kumaran | e4dad4f | 2010-11-21 14:36:14 +0000 | [diff] [blame] | 206 | |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 207 | |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 208 | .. method:: send_response_only(code, message=None) |
| 209 | |
| 210 | Sends the reponse header only, used for the purposes when ``100 |
Senthil Kumaran | e4dad4f | 2010-11-21 14:36:14 +0000 | [diff] [blame] | 211 | Continue`` response is sent by the server to the client. The headers not |
| 212 | buffered and sent directly the output stream.If the *message* is not |
| 213 | specified, the HTTP message corresponding the response *code* is sent. |
Senthil Kumaran | 0f476d4 | 2010-09-30 06:09:18 +0000 | [diff] [blame] | 214 | |
| 215 | .. versionadded:: 3.2 |
| 216 | |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 217 | .. method:: end_headers() |
| 218 | |
Senthil Kumaran | c7ae19b | 2011-05-09 23:25:02 +0800 | [diff] [blame] | 219 | Adds a blank line |
| 220 | (indicating the end of the HTTP headers in the response) |
Ezio Melotti | e9c7d6c | 2011-05-12 01:10:57 +0300 | [diff] [blame] | 221 | to the headers buffer and calls :meth:`flush_headers()`. |
Senthil Kumaran | e4dad4f | 2010-11-21 14:36:14 +0000 | [diff] [blame] | 222 | |
Ezio Melotti | e9c7d6c | 2011-05-12 01:10:57 +0300 | [diff] [blame] | 223 | .. versionchanged:: 3.2 |
| 224 | The buffered headers are written to the output stream. |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 225 | |
Senthil Kumaran | c7ae19b | 2011-05-09 23:25:02 +0800 | [diff] [blame] | 226 | .. method:: flush_headers() |
| 227 | |
| 228 | Finally send the headers to the output stream and flush the internal |
| 229 | headers buffer. |
| 230 | |
| 231 | .. versionadded:: 3.3 |
| 232 | |
Georg Brandl | 036490d | 2009-05-17 13:00:36 +0000 | [diff] [blame] | 233 | .. method:: log_request(code='-', size='-') |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 234 | |
| 235 | Logs an accepted (successful) request. *code* should specify the numeric |
| 236 | HTTP code associated with the response. If a size of the response is |
| 237 | available, then it should be passed as the *size* parameter. |
| 238 | |
| 239 | .. method:: log_error(...) |
| 240 | |
| 241 | Logs an error when a request cannot be fulfilled. By default, it passes |
| 242 | the message to :meth:`log_message`, so it takes the same arguments |
| 243 | (*format* and additional values). |
| 244 | |
| 245 | |
| 246 | .. method:: log_message(format, ...) |
| 247 | |
| 248 | Logs an arbitrary message to ``sys.stderr``. This is typically overridden |
| 249 | to create custom error logging mechanisms. The *format* argument is a |
| 250 | standard printf-style format string, where the additional arguments to |
| 251 | :meth:`log_message` are applied as inputs to the formatting. The client |
Senthil Kumaran | db727b4 | 2012-04-29 13:41:03 +0800 | [diff] [blame] | 252 | ip address and current date and time are prefixed to every message logged. |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 253 | |
| 254 | .. method:: version_string() |
| 255 | |
| 256 | Returns the server software's version string. This is a combination of the |
| 257 | :attr:`server_version` and :attr:`sys_version` class variables. |
| 258 | |
Georg Brandl | 036490d | 2009-05-17 13:00:36 +0000 | [diff] [blame] | 259 | .. method:: date_time_string(timestamp=None) |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 260 | |
Georg Brandl | 036490d | 2009-05-17 13:00:36 +0000 | [diff] [blame] | 261 | Returns the date and time given by *timestamp* (which must be None or in |
| 262 | the format returned by :func:`time.time`), formatted for a message |
| 263 | header. If *timestamp* is omitted, it uses the current date and time. |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 264 | |
| 265 | The result looks like ``'Sun, 06 Nov 1994 08:49:37 GMT'``. |
| 266 | |
| 267 | .. method:: log_date_time_string() |
| 268 | |
| 269 | Returns the current date and time, formatted for logging. |
| 270 | |
| 271 | .. method:: address_string() |
| 272 | |
Senthil Kumaran | 1aacba4 | 2012-04-29 12:51:54 +0800 | [diff] [blame] | 273 | Returns the client address. |
| 274 | |
| 275 | .. versionchanged:: 3.3 |
| 276 | Previously, a name lookup was performed. To avoid name resolution |
| 277 | delays, it now always returns the IP address. |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 278 | |
| 279 | |
| 280 | .. class:: SimpleHTTPRequestHandler(request, client_address, server) |
| 281 | |
| 282 | This class serves files from the current directory and below, directly |
| 283 | mapping the directory structure to HTTP requests. |
| 284 | |
| 285 | A lot of the work, such as parsing the request, is done by the base class |
| 286 | :class:`BaseHTTPRequestHandler`. This class implements the :func:`do_GET` |
| 287 | and :func:`do_HEAD` functions. |
| 288 | |
| 289 | The following are defined as class-level attributes of |
| 290 | :class:`SimpleHTTPRequestHandler`: |
| 291 | |
| 292 | .. attribute:: server_version |
| 293 | |
| 294 | This will be ``"SimpleHTTP/" + __version__``, where ``__version__`` is |
| 295 | defined at the module level. |
| 296 | |
| 297 | .. attribute:: extensions_map |
| 298 | |
| 299 | A dictionary mapping suffixes into MIME types. The default is |
| 300 | signified by an empty string, and is considered to be |
| 301 | ``application/octet-stream``. The mapping is used case-insensitively, |
| 302 | and so should contain only lower-cased keys. |
| 303 | |
| 304 | The :class:`SimpleHTTPRequestHandler` class defines the following methods: |
| 305 | |
| 306 | .. method:: do_HEAD() |
| 307 | |
| 308 | This method serves the ``'HEAD'`` request type: it sends the headers it |
| 309 | would send for the equivalent ``GET`` request. See the :meth:`do_GET` |
| 310 | method for a more complete explanation of the possible headers. |
| 311 | |
| 312 | .. method:: do_GET() |
| 313 | |
| 314 | The request is mapped to a local file by interpreting the request as a |
| 315 | path relative to the current working directory. |
| 316 | |
| 317 | If the request was mapped to a directory, the directory is checked for a |
| 318 | file named ``index.html`` or ``index.htm`` (in that order). If found, the |
| 319 | file's contents are returned; otherwise a directory listing is generated |
| 320 | by calling the :meth:`list_directory` method. This method uses |
| 321 | :func:`os.listdir` to scan the directory, and returns a ``404`` error |
| 322 | response if the :func:`listdir` fails. |
| 323 | |
| 324 | If the request was mapped to a file, it is opened and the contents are |
Antoine Pitrou | 62ab10a0 | 2011-10-12 20:10:51 +0200 | [diff] [blame] | 325 | returned. Any :exc:`OSError` exception in opening the requested file is |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 326 | mapped to a ``404``, ``'File not found'`` error. Otherwise, the content |
| 327 | type is guessed by calling the :meth:`guess_type` method, which in turn |
| 328 | uses the *extensions_map* variable. |
| 329 | |
| 330 | A ``'Content-type:'`` header with the guessed content type is output, |
| 331 | followed by a ``'Content-Length:'`` header with the file's size and a |
| 332 | ``'Last-Modified:'`` header with the file's modification time. |
| 333 | |
| 334 | Then follows a blank line signifying the end of the headers, and then the |
| 335 | contents of the file are output. If the file's MIME type starts with |
| 336 | ``text/`` the file is opened in text mode; otherwise binary mode is used. |
| 337 | |
Senthil Kumaran | 97db43b | 2010-06-16 16:41:11 +0000 | [diff] [blame] | 338 | For example usage, see the implementation of the :func:`test` function |
| 339 | invocation in the :mod:`http.server` module. |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 340 | |
Senthil Kumaran | 97db43b | 2010-06-16 16:41:11 +0000 | [diff] [blame] | 341 | |
Georg Brandl | 8971f74 | 2010-07-02 07:41:51 +0000 | [diff] [blame] | 342 | The :class:`SimpleHTTPRequestHandler` class can be used in the following |
| 343 | manner in order to create a very basic webserver serving files relative to |
| 344 | the current directory. :: |
Senthil Kumaran | 97db43b | 2010-06-16 16:41:11 +0000 | [diff] [blame] | 345 | |
Georg Brandl | 8971f74 | 2010-07-02 07:41:51 +0000 | [diff] [blame] | 346 | import http.server |
| 347 | import socketserver |
Senthil Kumaran | 97db43b | 2010-06-16 16:41:11 +0000 | [diff] [blame] | 348 | |
Georg Brandl | 8971f74 | 2010-07-02 07:41:51 +0000 | [diff] [blame] | 349 | PORT = 8000 |
Senthil Kumaran | 97db43b | 2010-06-16 16:41:11 +0000 | [diff] [blame] | 350 | |
Georg Brandl | 8971f74 | 2010-07-02 07:41:51 +0000 | [diff] [blame] | 351 | Handler = http.server.SimpleHTTPRequestHandler |
Senthil Kumaran | 97db43b | 2010-06-16 16:41:11 +0000 | [diff] [blame] | 352 | |
Georg Brandl | 8971f74 | 2010-07-02 07:41:51 +0000 | [diff] [blame] | 353 | httpd = socketserver.TCPServer(("", PORT), Handler) |
Senthil Kumaran | 97db43b | 2010-06-16 16:41:11 +0000 | [diff] [blame] | 354 | |
Georg Brandl | 8971f74 | 2010-07-02 07:41:51 +0000 | [diff] [blame] | 355 | print("serving at port", PORT) |
| 356 | httpd.serve_forever() |
| 357 | |
Georg Brandl | f68798b | 2010-07-03 10:22:10 +0000 | [diff] [blame] | 358 | :mod:`http.server` can also be invoked directly using the :option:`-m` |
R David Murray | e7bade5 | 2012-04-11 20:13:25 -0400 | [diff] [blame] | 359 | switch of the interpreter with a ``port number`` argument. Similar to |
Georg Brandl | 8971f74 | 2010-07-02 07:41:51 +0000 | [diff] [blame] | 360 | the previous example, this serves files relative to the current directory. :: |
Senthil Kumaran | 97db43b | 2010-06-16 16:41:11 +0000 | [diff] [blame] | 361 | |
| 362 | python -m http.server 8000 |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 363 | |
Georg Brandl | 8971f74 | 2010-07-02 07:41:51 +0000 | [diff] [blame] | 364 | |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 365 | .. class:: CGIHTTPRequestHandler(request, client_address, server) |
| 366 | |
| 367 | This class is used to serve either files or output of CGI scripts from the |
| 368 | current directory and below. Note that mapping HTTP hierarchic structure to |
| 369 | local directory structure is exactly as in :class:`SimpleHTTPRequestHandler`. |
| 370 | |
| 371 | .. note:: |
| 372 | |
| 373 | CGI scripts run by the :class:`CGIHTTPRequestHandler` class cannot execute |
| 374 | redirects (HTTP code 302), because code 200 (script output follows) is |
| 375 | sent prior to execution of the CGI script. This pre-empts the status |
| 376 | code. |
| 377 | |
| 378 | The class will however, run the CGI script, instead of serving it as a file, |
| 379 | if it guesses it to be a CGI script. Only directory-based CGI are used --- |
| 380 | the other common server configuration is to treat special extensions as |
| 381 | denoting CGI scripts. |
| 382 | |
| 383 | The :func:`do_GET` and :func:`do_HEAD` functions are modified to run CGI scripts |
| 384 | and serve the output, instead of serving files, if the request leads to |
| 385 | somewhere below the ``cgi_directories`` path. |
| 386 | |
| 387 | The :class:`CGIHTTPRequestHandler` defines the following data member: |
| 388 | |
| 389 | .. attribute:: cgi_directories |
| 390 | |
| 391 | This defaults to ``['/cgi-bin', '/htbin']`` and describes directories to |
| 392 | treat as containing CGI scripts. |
| 393 | |
| 394 | The :class:`CGIHTTPRequestHandler` defines the following method: |
| 395 | |
| 396 | .. method:: do_POST() |
| 397 | |
| 398 | This method serves the ``'POST'`` request type, only allowed for CGI |
| 399 | scripts. Error 501, "Can only POST to CGI scripts", is output when trying |
| 400 | to POST to a non-CGI url. |
| 401 | |
| 402 | Note that CGI scripts will be run with UID of user nobody, for security |
| 403 | reasons. Problems with the CGI script will be translated to error 403. |
Senthil Kumaran | 1251faf | 2012-06-03 16:15:54 +0800 | [diff] [blame] | 404 | |
| 405 | :class:`CGIHTTPRequestHandler` can be enabled in the command line by passing |
| 406 | the ``--cgi`` option.:: |
| 407 | |
| 408 | python -m http.server --cgi 8000 |
| 409 | |