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