17db96d56Sopenharmony_ci:mod:`http.server` --- HTTP servers
27db96d56Sopenharmony_ci===================================
37db96d56Sopenharmony_ci
47db96d56Sopenharmony_ci.. module:: http.server
57db96d56Sopenharmony_ci   :synopsis: HTTP server and request handlers.
67db96d56Sopenharmony_ci
77db96d56Sopenharmony_ci**Source code:** :source:`Lib/http/server.py`
87db96d56Sopenharmony_ci
97db96d56Sopenharmony_ci.. index::
107db96d56Sopenharmony_ci   pair: WWW; server
117db96d56Sopenharmony_ci   pair: HTTP; protocol
127db96d56Sopenharmony_ci   single: URL
137db96d56Sopenharmony_ci   single: httpd
147db96d56Sopenharmony_ci
157db96d56Sopenharmony_ci--------------
167db96d56Sopenharmony_ci
177db96d56Sopenharmony_ciThis module defines classes for implementing HTTP servers.
187db96d56Sopenharmony_ci
197db96d56Sopenharmony_ci
207db96d56Sopenharmony_ci.. warning::
217db96d56Sopenharmony_ci
227db96d56Sopenharmony_ci    :mod:`http.server` is not recommended for production. It only implements
237db96d56Sopenharmony_ci    :ref:`basic security checks <http.server-security>`.
247db96d56Sopenharmony_ci
257db96d56Sopenharmony_ci.. include:: ../includes/wasm-notavail.rst
267db96d56Sopenharmony_ci
277db96d56Sopenharmony_ciOne class, :class:`HTTPServer`, is a :class:`socketserver.TCPServer` subclass.
287db96d56Sopenharmony_ciIt creates and listens at the HTTP socket, dispatching the requests to a
297db96d56Sopenharmony_cihandler.  Code to create and run the server looks like this::
307db96d56Sopenharmony_ci
317db96d56Sopenharmony_ci   def run(server_class=HTTPServer, handler_class=BaseHTTPRequestHandler):
327db96d56Sopenharmony_ci       server_address = ('', 8000)
337db96d56Sopenharmony_ci       httpd = server_class(server_address, handler_class)
347db96d56Sopenharmony_ci       httpd.serve_forever()
357db96d56Sopenharmony_ci
367db96d56Sopenharmony_ci
377db96d56Sopenharmony_ci.. class:: HTTPServer(server_address, RequestHandlerClass)
387db96d56Sopenharmony_ci
397db96d56Sopenharmony_ci   This class builds on the :class:`~socketserver.TCPServer` class by storing
407db96d56Sopenharmony_ci   the server address as instance variables named :attr:`server_name` and
417db96d56Sopenharmony_ci   :attr:`server_port`. The server is accessible by the handler, typically
427db96d56Sopenharmony_ci   through the handler's :attr:`server` instance variable.
437db96d56Sopenharmony_ci
447db96d56Sopenharmony_ci.. class:: ThreadingHTTPServer(server_address, RequestHandlerClass)
457db96d56Sopenharmony_ci
467db96d56Sopenharmony_ci   This class is identical to HTTPServer but uses threads to handle
477db96d56Sopenharmony_ci   requests by using the :class:`~socketserver.ThreadingMixIn`. This
487db96d56Sopenharmony_ci   is useful to handle web browsers pre-opening sockets, on which
497db96d56Sopenharmony_ci   :class:`HTTPServer` would wait indefinitely.
507db96d56Sopenharmony_ci
517db96d56Sopenharmony_ci   .. versionadded:: 3.7
527db96d56Sopenharmony_ci
537db96d56Sopenharmony_ci
547db96d56Sopenharmony_ciThe :class:`HTTPServer` and :class:`ThreadingHTTPServer` must be given
557db96d56Sopenharmony_cia *RequestHandlerClass* on instantiation, of which this module
567db96d56Sopenharmony_ciprovides three different variants:
577db96d56Sopenharmony_ci
587db96d56Sopenharmony_ci.. class:: BaseHTTPRequestHandler(request, client_address, server)
597db96d56Sopenharmony_ci
607db96d56Sopenharmony_ci   This class is used to handle the HTTP requests that arrive at the server.  By
617db96d56Sopenharmony_ci   itself, it cannot respond to any actual HTTP requests; it must be subclassed
627db96d56Sopenharmony_ci   to handle each request method (e.g. GET or POST).
637db96d56Sopenharmony_ci   :class:`BaseHTTPRequestHandler` provides a number of class and instance
647db96d56Sopenharmony_ci   variables, and methods for use by subclasses.
657db96d56Sopenharmony_ci
667db96d56Sopenharmony_ci   The handler will parse the request and the headers, then call a method
677db96d56Sopenharmony_ci   specific to the request type. The method name is constructed from the
687db96d56Sopenharmony_ci   request. For example, for the request method ``SPAM``, the :meth:`do_SPAM`
697db96d56Sopenharmony_ci   method will be called with no arguments. All of the relevant information is
707db96d56Sopenharmony_ci   stored in instance variables of the handler.  Subclasses should not need to
717db96d56Sopenharmony_ci   override or extend the :meth:`__init__` method.
727db96d56Sopenharmony_ci
737db96d56Sopenharmony_ci   :class:`BaseHTTPRequestHandler` has the following instance variables:
747db96d56Sopenharmony_ci
757db96d56Sopenharmony_ci   .. attribute:: client_address
767db96d56Sopenharmony_ci
777db96d56Sopenharmony_ci      Contains a tuple of the form ``(host, port)`` referring to the client's
787db96d56Sopenharmony_ci      address.
797db96d56Sopenharmony_ci
807db96d56Sopenharmony_ci   .. attribute:: server
817db96d56Sopenharmony_ci
827db96d56Sopenharmony_ci      Contains the server instance.
837db96d56Sopenharmony_ci
847db96d56Sopenharmony_ci   .. attribute:: close_connection
857db96d56Sopenharmony_ci
867db96d56Sopenharmony_ci      Boolean that should be set before :meth:`handle_one_request` returns,
877db96d56Sopenharmony_ci      indicating if another request may be expected, or if the connection should
887db96d56Sopenharmony_ci      be shut down.
897db96d56Sopenharmony_ci
907db96d56Sopenharmony_ci   .. attribute:: requestline
917db96d56Sopenharmony_ci
927db96d56Sopenharmony_ci      Contains the string representation of the HTTP request line. The
937db96d56Sopenharmony_ci      terminating CRLF is stripped. This attribute should be set by
947db96d56Sopenharmony_ci      :meth:`handle_one_request`. If no valid request line was processed, it
957db96d56Sopenharmony_ci      should be set to the empty string.
967db96d56Sopenharmony_ci
977db96d56Sopenharmony_ci   .. attribute:: command
987db96d56Sopenharmony_ci
997db96d56Sopenharmony_ci      Contains the command (request type). For example, ``'GET'``.
1007db96d56Sopenharmony_ci
1017db96d56Sopenharmony_ci   .. attribute:: path
1027db96d56Sopenharmony_ci
1037db96d56Sopenharmony_ci      Contains the request path. If query component of the URL is present,
1047db96d56Sopenharmony_ci      then ``path`` includes the query. Using the terminology of :rfc:`3986`,
1057db96d56Sopenharmony_ci      ``path`` here includes ``hier-part`` and the ``query``.
1067db96d56Sopenharmony_ci
1077db96d56Sopenharmony_ci   .. attribute:: request_version
1087db96d56Sopenharmony_ci
1097db96d56Sopenharmony_ci      Contains the version string from the request. For example, ``'HTTP/1.0'``.
1107db96d56Sopenharmony_ci
1117db96d56Sopenharmony_ci   .. attribute:: headers
1127db96d56Sopenharmony_ci
1137db96d56Sopenharmony_ci      Holds an instance of the class specified by the :attr:`MessageClass` class
1147db96d56Sopenharmony_ci      variable. This instance parses and manages the headers in the HTTP
1157db96d56Sopenharmony_ci      request. The :func:`~http.client.parse_headers` function from
1167db96d56Sopenharmony_ci      :mod:`http.client` is used to parse the headers and it requires that the
1177db96d56Sopenharmony_ci      HTTP request provide a valid :rfc:`2822` style header.
1187db96d56Sopenharmony_ci
1197db96d56Sopenharmony_ci   .. attribute:: rfile
1207db96d56Sopenharmony_ci
1217db96d56Sopenharmony_ci      An :class:`io.BufferedIOBase` input stream, ready to read from
1227db96d56Sopenharmony_ci      the start of the optional input data.
1237db96d56Sopenharmony_ci
1247db96d56Sopenharmony_ci   .. attribute:: wfile
1257db96d56Sopenharmony_ci
1267db96d56Sopenharmony_ci      Contains the output stream for writing a response back to the
1277db96d56Sopenharmony_ci      client. Proper adherence to the HTTP protocol must be used when writing to
1287db96d56Sopenharmony_ci      this stream in order to achieve successful interoperation with HTTP
1297db96d56Sopenharmony_ci      clients.
1307db96d56Sopenharmony_ci
1317db96d56Sopenharmony_ci      .. versionchanged:: 3.6
1327db96d56Sopenharmony_ci         This is an :class:`io.BufferedIOBase` stream.
1337db96d56Sopenharmony_ci
1347db96d56Sopenharmony_ci   :class:`BaseHTTPRequestHandler` has the following attributes:
1357db96d56Sopenharmony_ci
1367db96d56Sopenharmony_ci   .. attribute:: server_version
1377db96d56Sopenharmony_ci
1387db96d56Sopenharmony_ci      Specifies the server software version.  You may want to override this. The
1397db96d56Sopenharmony_ci      format is multiple whitespace-separated strings, where each string is of
1407db96d56Sopenharmony_ci      the form name[/version]. For example, ``'BaseHTTP/0.2'``.
1417db96d56Sopenharmony_ci
1427db96d56Sopenharmony_ci   .. attribute:: sys_version
1437db96d56Sopenharmony_ci
1447db96d56Sopenharmony_ci      Contains the Python system version, in a form usable by the
1457db96d56Sopenharmony_ci      :attr:`version_string` method and the :attr:`server_version` class
1467db96d56Sopenharmony_ci      variable. For example, ``'Python/1.4'``.
1477db96d56Sopenharmony_ci
1487db96d56Sopenharmony_ci   .. attribute:: error_message_format
1497db96d56Sopenharmony_ci
1507db96d56Sopenharmony_ci      Specifies a format string that should be used by :meth:`send_error` method
1517db96d56Sopenharmony_ci      for building an error response to the client. The string is filled by
1527db96d56Sopenharmony_ci      default with variables from :attr:`responses` based on the status code
1537db96d56Sopenharmony_ci      that passed to :meth:`send_error`.
1547db96d56Sopenharmony_ci
1557db96d56Sopenharmony_ci   .. attribute:: error_content_type
1567db96d56Sopenharmony_ci
1577db96d56Sopenharmony_ci      Specifies the Content-Type HTTP header of error responses sent to the
1587db96d56Sopenharmony_ci      client.  The default value is ``'text/html'``.
1597db96d56Sopenharmony_ci
1607db96d56Sopenharmony_ci   .. attribute:: protocol_version
1617db96d56Sopenharmony_ci
1627db96d56Sopenharmony_ci      Specifies the HTTP version to which the server is conformant. It is sent
1637db96d56Sopenharmony_ci      in responses to let the client know the server's communication
1647db96d56Sopenharmony_ci      capabilities for future requests. If set to
1657db96d56Sopenharmony_ci      ``'HTTP/1.1'``, the server will permit HTTP persistent connections;
1667db96d56Sopenharmony_ci      however, your server *must* then include an accurate ``Content-Length``
1677db96d56Sopenharmony_ci      header (using :meth:`send_header`) in all of its responses to clients.
1687db96d56Sopenharmony_ci      For backwards compatibility, the setting defaults to ``'HTTP/1.0'``.
1697db96d56Sopenharmony_ci
1707db96d56Sopenharmony_ci   .. attribute:: MessageClass
1717db96d56Sopenharmony_ci
1727db96d56Sopenharmony_ci      Specifies an :class:`email.message.Message`\ -like class to parse HTTP
1737db96d56Sopenharmony_ci      headers.  Typically, this is not overridden, and it defaults to
1747db96d56Sopenharmony_ci      :class:`http.client.HTTPMessage`.
1757db96d56Sopenharmony_ci
1767db96d56Sopenharmony_ci   .. attribute:: responses
1777db96d56Sopenharmony_ci
1787db96d56Sopenharmony_ci      This attribute contains a mapping of error code integers to two-element tuples
1797db96d56Sopenharmony_ci      containing a short and long message. For example, ``{code: (shortmessage,
1807db96d56Sopenharmony_ci      longmessage)}``. The *shortmessage* is usually used as the *message* key in an
1817db96d56Sopenharmony_ci      error response, and *longmessage* as the *explain* key.  It is used by
1827db96d56Sopenharmony_ci      :meth:`send_response_only` and :meth:`send_error` methods.
1837db96d56Sopenharmony_ci
1847db96d56Sopenharmony_ci   A :class:`BaseHTTPRequestHandler` instance has the following methods:
1857db96d56Sopenharmony_ci
1867db96d56Sopenharmony_ci   .. method:: handle()
1877db96d56Sopenharmony_ci
1887db96d56Sopenharmony_ci      Calls :meth:`handle_one_request` once (or, if persistent connections are
1897db96d56Sopenharmony_ci      enabled, multiple times) to handle incoming HTTP requests. You should
1907db96d56Sopenharmony_ci      never need to override it; instead, implement appropriate :meth:`do_\*`
1917db96d56Sopenharmony_ci      methods.
1927db96d56Sopenharmony_ci
1937db96d56Sopenharmony_ci   .. method:: handle_one_request()
1947db96d56Sopenharmony_ci
1957db96d56Sopenharmony_ci      This method will parse and dispatch the request to the appropriate
1967db96d56Sopenharmony_ci      :meth:`do_\*` method.  You should never need to override it.
1977db96d56Sopenharmony_ci
1987db96d56Sopenharmony_ci   .. method:: handle_expect_100()
1997db96d56Sopenharmony_ci
2007db96d56Sopenharmony_ci      When an HTTP/1.1 conformant server receives an ``Expect: 100-continue``
2017db96d56Sopenharmony_ci      request header it responds back with a ``100 Continue`` followed by ``200
2027db96d56Sopenharmony_ci      OK`` headers.
2037db96d56Sopenharmony_ci      This method can be overridden to raise an error if the server does not
2047db96d56Sopenharmony_ci      want the client to continue.  For e.g. server can choose to send ``417
2057db96d56Sopenharmony_ci      Expectation Failed`` as a response header and ``return False``.
2067db96d56Sopenharmony_ci
2077db96d56Sopenharmony_ci      .. versionadded:: 3.2
2087db96d56Sopenharmony_ci
2097db96d56Sopenharmony_ci   .. method:: send_error(code, message=None, explain=None)
2107db96d56Sopenharmony_ci
2117db96d56Sopenharmony_ci      Sends and logs a complete error reply to the client. The numeric *code*
2127db96d56Sopenharmony_ci      specifies the HTTP error code, with *message* as an optional, short, human
2137db96d56Sopenharmony_ci      readable description of the error.  The *explain* argument can be used to
2147db96d56Sopenharmony_ci      provide more detailed information about the error; it will be formatted
2157db96d56Sopenharmony_ci      using the :attr:`error_message_format` attribute and emitted, after
2167db96d56Sopenharmony_ci      a complete set of headers, as the response body.  The :attr:`responses`
2177db96d56Sopenharmony_ci      attribute holds the default values for *message* and *explain* that
2187db96d56Sopenharmony_ci      will be used if no value is provided; for unknown codes the default value
2197db96d56Sopenharmony_ci      for both is the string ``???``. The body will be empty if the method is
2207db96d56Sopenharmony_ci      HEAD or the response code is one of the following: ``1xx``,
2217db96d56Sopenharmony_ci      ``204 No Content``, ``205 Reset Content``, ``304 Not Modified``.
2227db96d56Sopenharmony_ci
2237db96d56Sopenharmony_ci      .. versionchanged:: 3.4
2247db96d56Sopenharmony_ci         The error response includes a Content-Length header.
2257db96d56Sopenharmony_ci         Added the *explain* argument.
2267db96d56Sopenharmony_ci
2277db96d56Sopenharmony_ci   .. method:: send_response(code, message=None)
2287db96d56Sopenharmony_ci
2297db96d56Sopenharmony_ci      Adds a response header to the headers buffer and logs the accepted
2307db96d56Sopenharmony_ci      request. The HTTP response line is written to the internal buffer,
2317db96d56Sopenharmony_ci      followed by *Server* and *Date* headers. The values for these two headers
2327db96d56Sopenharmony_ci      are picked up from the :meth:`version_string` and
2337db96d56Sopenharmony_ci      :meth:`date_time_string` methods, respectively. If the server does not
2347db96d56Sopenharmony_ci      intend to send any other headers using the :meth:`send_header` method,
2357db96d56Sopenharmony_ci      then :meth:`send_response` should be followed by an :meth:`end_headers`
2367db96d56Sopenharmony_ci      call.
2377db96d56Sopenharmony_ci
2387db96d56Sopenharmony_ci      .. versionchanged:: 3.3
2397db96d56Sopenharmony_ci         Headers are stored to an internal buffer and :meth:`end_headers`
2407db96d56Sopenharmony_ci         needs to be called explicitly.
2417db96d56Sopenharmony_ci
2427db96d56Sopenharmony_ci   .. method:: send_header(keyword, value)
2437db96d56Sopenharmony_ci
2447db96d56Sopenharmony_ci      Adds the HTTP header to an internal buffer which will be written to the
2457db96d56Sopenharmony_ci      output stream when either :meth:`end_headers` or :meth:`flush_headers` is
2467db96d56Sopenharmony_ci      invoked. *keyword* should specify the header keyword, with *value*
2477db96d56Sopenharmony_ci      specifying its value. Note that, after the send_header calls are done,
2487db96d56Sopenharmony_ci      :meth:`end_headers` MUST BE called in order to complete the operation.
2497db96d56Sopenharmony_ci
2507db96d56Sopenharmony_ci      .. versionchanged:: 3.2
2517db96d56Sopenharmony_ci         Headers are stored in an internal buffer.
2527db96d56Sopenharmony_ci
2537db96d56Sopenharmony_ci   .. method:: send_response_only(code, message=None)
2547db96d56Sopenharmony_ci
2557db96d56Sopenharmony_ci      Sends the response header only, used for the purposes when ``100
2567db96d56Sopenharmony_ci      Continue`` response is sent by the server to the client. The headers not
2577db96d56Sopenharmony_ci      buffered and sent directly the output stream.If the *message* is not
2587db96d56Sopenharmony_ci      specified, the HTTP message corresponding the response *code*  is sent.
2597db96d56Sopenharmony_ci
2607db96d56Sopenharmony_ci      .. versionadded:: 3.2
2617db96d56Sopenharmony_ci
2627db96d56Sopenharmony_ci   .. method:: end_headers()
2637db96d56Sopenharmony_ci
2647db96d56Sopenharmony_ci      Adds a blank line
2657db96d56Sopenharmony_ci      (indicating the end of the HTTP headers in the response)
2667db96d56Sopenharmony_ci      to the headers buffer and calls :meth:`flush_headers()`.
2677db96d56Sopenharmony_ci
2687db96d56Sopenharmony_ci      .. versionchanged:: 3.2
2697db96d56Sopenharmony_ci         The buffered headers are written to the output stream.
2707db96d56Sopenharmony_ci
2717db96d56Sopenharmony_ci   .. method:: flush_headers()
2727db96d56Sopenharmony_ci
2737db96d56Sopenharmony_ci      Finally send the headers to the output stream and flush the internal
2747db96d56Sopenharmony_ci      headers buffer.
2757db96d56Sopenharmony_ci
2767db96d56Sopenharmony_ci      .. versionadded:: 3.3
2777db96d56Sopenharmony_ci
2787db96d56Sopenharmony_ci   .. method:: log_request(code='-', size='-')
2797db96d56Sopenharmony_ci
2807db96d56Sopenharmony_ci      Logs an accepted (successful) request. *code* should specify the numeric
2817db96d56Sopenharmony_ci      HTTP code associated with the response. If a size of the response is
2827db96d56Sopenharmony_ci      available, then it should be passed as the *size* parameter.
2837db96d56Sopenharmony_ci
2847db96d56Sopenharmony_ci   .. method:: log_error(...)
2857db96d56Sopenharmony_ci
2867db96d56Sopenharmony_ci      Logs an error when a request cannot be fulfilled. By default, it passes
2877db96d56Sopenharmony_ci      the message to :meth:`log_message`, so it takes the same arguments
2887db96d56Sopenharmony_ci      (*format* and additional values).
2897db96d56Sopenharmony_ci
2907db96d56Sopenharmony_ci
2917db96d56Sopenharmony_ci   .. method:: log_message(format, ...)
2927db96d56Sopenharmony_ci
2937db96d56Sopenharmony_ci      Logs an arbitrary message to ``sys.stderr``. This is typically overridden
2947db96d56Sopenharmony_ci      to create custom error logging mechanisms. The *format* argument is a
2957db96d56Sopenharmony_ci      standard printf-style format string, where the additional arguments to
2967db96d56Sopenharmony_ci      :meth:`log_message` are applied as inputs to the formatting. The client
2977db96d56Sopenharmony_ci      ip address and current date and time are prefixed to every message logged.
2987db96d56Sopenharmony_ci
2997db96d56Sopenharmony_ci   .. method:: version_string()
3007db96d56Sopenharmony_ci
3017db96d56Sopenharmony_ci      Returns the server software's version string. This is a combination of the
3027db96d56Sopenharmony_ci      :attr:`server_version` and :attr:`sys_version` attributes.
3037db96d56Sopenharmony_ci
3047db96d56Sopenharmony_ci   .. method:: date_time_string(timestamp=None)
3057db96d56Sopenharmony_ci
3067db96d56Sopenharmony_ci      Returns the date and time given by *timestamp* (which must be ``None`` or in
3077db96d56Sopenharmony_ci      the format returned by :func:`time.time`), formatted for a message
3087db96d56Sopenharmony_ci      header. If *timestamp* is omitted, it uses the current date and time.
3097db96d56Sopenharmony_ci
3107db96d56Sopenharmony_ci      The result looks like ``'Sun, 06 Nov 1994 08:49:37 GMT'``.
3117db96d56Sopenharmony_ci
3127db96d56Sopenharmony_ci   .. method:: log_date_time_string()
3137db96d56Sopenharmony_ci
3147db96d56Sopenharmony_ci      Returns the current date and time, formatted for logging.
3157db96d56Sopenharmony_ci
3167db96d56Sopenharmony_ci   .. method:: address_string()
3177db96d56Sopenharmony_ci
3187db96d56Sopenharmony_ci      Returns the client address.
3197db96d56Sopenharmony_ci
3207db96d56Sopenharmony_ci      .. versionchanged:: 3.3
3217db96d56Sopenharmony_ci         Previously, a name lookup was performed. To avoid name resolution
3227db96d56Sopenharmony_ci         delays, it now always returns the IP address.
3237db96d56Sopenharmony_ci
3247db96d56Sopenharmony_ci
3257db96d56Sopenharmony_ci.. class:: SimpleHTTPRequestHandler(request, client_address, server, directory=None)
3267db96d56Sopenharmony_ci
3277db96d56Sopenharmony_ci   This class serves files from the directory *directory* and below,
3287db96d56Sopenharmony_ci   or the current directory if *directory* is not provided, directly
3297db96d56Sopenharmony_ci   mapping the directory structure to HTTP requests.
3307db96d56Sopenharmony_ci
3317db96d56Sopenharmony_ci   .. versionadded:: 3.7
3327db96d56Sopenharmony_ci      The *directory* parameter.
3337db96d56Sopenharmony_ci
3347db96d56Sopenharmony_ci   .. versionchanged:: 3.9
3357db96d56Sopenharmony_ci      The *directory* parameter accepts a :term:`path-like object`.
3367db96d56Sopenharmony_ci
3377db96d56Sopenharmony_ci   A lot of the work, such as parsing the request, is done by the base class
3387db96d56Sopenharmony_ci   :class:`BaseHTTPRequestHandler`.  This class implements the :func:`do_GET`
3397db96d56Sopenharmony_ci   and :func:`do_HEAD` functions.
3407db96d56Sopenharmony_ci
3417db96d56Sopenharmony_ci   The following are defined as class-level attributes of
3427db96d56Sopenharmony_ci   :class:`SimpleHTTPRequestHandler`:
3437db96d56Sopenharmony_ci
3447db96d56Sopenharmony_ci   .. attribute:: server_version
3457db96d56Sopenharmony_ci
3467db96d56Sopenharmony_ci      This will be ``"SimpleHTTP/" + __version__``, where ``__version__`` is
3477db96d56Sopenharmony_ci      defined at the module level.
3487db96d56Sopenharmony_ci
3497db96d56Sopenharmony_ci   .. attribute:: extensions_map
3507db96d56Sopenharmony_ci
3517db96d56Sopenharmony_ci      A dictionary mapping suffixes into MIME types, contains custom overrides
3527db96d56Sopenharmony_ci      for the default system mappings. The mapping is used case-insensitively,
3537db96d56Sopenharmony_ci      and so should contain only lower-cased keys.
3547db96d56Sopenharmony_ci
3557db96d56Sopenharmony_ci      .. versionchanged:: 3.9
3567db96d56Sopenharmony_ci         This dictionary is no longer filled with the default system mappings,
3577db96d56Sopenharmony_ci         but only contains overrides.
3587db96d56Sopenharmony_ci
3597db96d56Sopenharmony_ci   The :class:`SimpleHTTPRequestHandler` class defines the following methods:
3607db96d56Sopenharmony_ci
3617db96d56Sopenharmony_ci   .. method:: do_HEAD()
3627db96d56Sopenharmony_ci
3637db96d56Sopenharmony_ci      This method serves the ``'HEAD'`` request type: it sends the headers it
3647db96d56Sopenharmony_ci      would send for the equivalent ``GET`` request. See the :meth:`do_GET`
3657db96d56Sopenharmony_ci      method for a more complete explanation of the possible headers.
3667db96d56Sopenharmony_ci
3677db96d56Sopenharmony_ci   .. method:: do_GET()
3687db96d56Sopenharmony_ci
3697db96d56Sopenharmony_ci      The request is mapped to a local file by interpreting the request as a
3707db96d56Sopenharmony_ci      path relative to the current working directory.
3717db96d56Sopenharmony_ci
3727db96d56Sopenharmony_ci      If the request was mapped to a directory, the directory is checked for a
3737db96d56Sopenharmony_ci      file named ``index.html`` or ``index.htm`` (in that order). If found, the
3747db96d56Sopenharmony_ci      file's contents are returned; otherwise a directory listing is generated
3757db96d56Sopenharmony_ci      by calling the :meth:`list_directory` method. This method uses
3767db96d56Sopenharmony_ci      :func:`os.listdir` to scan the directory, and returns a ``404`` error
3777db96d56Sopenharmony_ci      response if the :func:`~os.listdir` fails.
3787db96d56Sopenharmony_ci
3797db96d56Sopenharmony_ci      If the request was mapped to a file, it is opened. Any :exc:`OSError`
3807db96d56Sopenharmony_ci      exception in opening the requested file is mapped to a ``404``,
3817db96d56Sopenharmony_ci      ``'File not found'`` error. If there was a ``'If-Modified-Since'``
3827db96d56Sopenharmony_ci      header in the request, and the file was not modified after this time,
3837db96d56Sopenharmony_ci      a ``304``, ``'Not Modified'`` response is sent. Otherwise, the content
3847db96d56Sopenharmony_ci      type is guessed by calling the :meth:`guess_type` method, which in turn
3857db96d56Sopenharmony_ci      uses the *extensions_map* variable, and the file contents are returned.
3867db96d56Sopenharmony_ci
3877db96d56Sopenharmony_ci      A ``'Content-type:'`` header with the guessed content type is output,
3887db96d56Sopenharmony_ci      followed by a ``'Content-Length:'`` header with the file's size and a
3897db96d56Sopenharmony_ci      ``'Last-Modified:'`` header with the file's modification time.
3907db96d56Sopenharmony_ci
3917db96d56Sopenharmony_ci      Then follows a blank line signifying the end of the headers, and then the
3927db96d56Sopenharmony_ci      contents of the file are output. If the file's MIME type starts with
3937db96d56Sopenharmony_ci      ``text/`` the file is opened in text mode; otherwise binary mode is used.
3947db96d56Sopenharmony_ci
3957db96d56Sopenharmony_ci      For example usage, see the implementation of the ``test`` function
3967db96d56Sopenharmony_ci      in :source:`Lib/http/server.py`.
3977db96d56Sopenharmony_ci
3987db96d56Sopenharmony_ci      .. versionchanged:: 3.7
3997db96d56Sopenharmony_ci         Support of the ``'If-Modified-Since'`` header.
4007db96d56Sopenharmony_ci
4017db96d56Sopenharmony_ciThe :class:`SimpleHTTPRequestHandler` class can be used in the following
4027db96d56Sopenharmony_cimanner in order to create a very basic webserver serving files relative to
4037db96d56Sopenharmony_cithe current directory::
4047db96d56Sopenharmony_ci
4057db96d56Sopenharmony_ci   import http.server
4067db96d56Sopenharmony_ci   import socketserver
4077db96d56Sopenharmony_ci
4087db96d56Sopenharmony_ci   PORT = 8000
4097db96d56Sopenharmony_ci
4107db96d56Sopenharmony_ci   Handler = http.server.SimpleHTTPRequestHandler
4117db96d56Sopenharmony_ci
4127db96d56Sopenharmony_ci   with socketserver.TCPServer(("", PORT), Handler) as httpd:
4137db96d56Sopenharmony_ci       print("serving at port", PORT)
4147db96d56Sopenharmony_ci       httpd.serve_forever()
4157db96d56Sopenharmony_ci
4167db96d56Sopenharmony_ci.. _http-server-cli:
4177db96d56Sopenharmony_ci
4187db96d56Sopenharmony_ci:mod:`http.server` can also be invoked directly using the :option:`-m`
4197db96d56Sopenharmony_ciswitch of the interpreter.  Similar to
4207db96d56Sopenharmony_cithe previous example, this serves files relative to the current directory::
4217db96d56Sopenharmony_ci
4227db96d56Sopenharmony_ci        python -m http.server
4237db96d56Sopenharmony_ci
4247db96d56Sopenharmony_ciThe server listens to port 8000 by default. The default can be overridden
4257db96d56Sopenharmony_ciby passing the desired port number as an argument::
4267db96d56Sopenharmony_ci
4277db96d56Sopenharmony_ci        python -m http.server 9000
4287db96d56Sopenharmony_ci
4297db96d56Sopenharmony_ciBy default, the server binds itself to all interfaces.  The option ``-b/--bind``
4307db96d56Sopenharmony_cispecifies a specific address to which it should bind. Both IPv4 and IPv6
4317db96d56Sopenharmony_ciaddresses are supported. For example, the following command causes the server
4327db96d56Sopenharmony_cito bind to localhost only::
4337db96d56Sopenharmony_ci
4347db96d56Sopenharmony_ci        python -m http.server --bind 127.0.0.1
4357db96d56Sopenharmony_ci
4367db96d56Sopenharmony_ci.. versionadded:: 3.4
4377db96d56Sopenharmony_ci    ``--bind`` argument was introduced.
4387db96d56Sopenharmony_ci
4397db96d56Sopenharmony_ci.. versionadded:: 3.8
4407db96d56Sopenharmony_ci    ``--bind`` argument enhanced to support IPv6
4417db96d56Sopenharmony_ci
4427db96d56Sopenharmony_ciBy default, the server uses the current directory. The option ``-d/--directory``
4437db96d56Sopenharmony_cispecifies a directory to which it should serve the files. For example,
4447db96d56Sopenharmony_cithe following command uses a specific directory::
4457db96d56Sopenharmony_ci
4467db96d56Sopenharmony_ci        python -m http.server --directory /tmp/
4477db96d56Sopenharmony_ci
4487db96d56Sopenharmony_ci.. versionadded:: 3.7
4497db96d56Sopenharmony_ci    ``--directory`` argument was introduced.
4507db96d56Sopenharmony_ci
4517db96d56Sopenharmony_ciBy default, the server is conformant to HTTP/1.0. The option ``-p/--protocol``
4527db96d56Sopenharmony_cispecifies the HTTP version to which the server is conformant. For example, the
4537db96d56Sopenharmony_cifollowing command runs an HTTP/1.1 conformant server::
4547db96d56Sopenharmony_ci
4557db96d56Sopenharmony_ci        python -m http.server --protocol HTTP/1.1
4567db96d56Sopenharmony_ci
4577db96d56Sopenharmony_ci.. versionadded:: 3.11
4587db96d56Sopenharmony_ci    ``--protocol`` argument was introduced.
4597db96d56Sopenharmony_ci
4607db96d56Sopenharmony_ci.. class:: CGIHTTPRequestHandler(request, client_address, server)
4617db96d56Sopenharmony_ci
4627db96d56Sopenharmony_ci   This class is used to serve either files or output of CGI scripts from the
4637db96d56Sopenharmony_ci   current directory and below. Note that mapping HTTP hierarchic structure to
4647db96d56Sopenharmony_ci   local directory structure is exactly as in :class:`SimpleHTTPRequestHandler`.
4657db96d56Sopenharmony_ci
4667db96d56Sopenharmony_ci   .. note::
4677db96d56Sopenharmony_ci
4687db96d56Sopenharmony_ci      CGI scripts run by the :class:`CGIHTTPRequestHandler` class cannot execute
4697db96d56Sopenharmony_ci      redirects (HTTP code 302), because code 200 (script output follows) is
4707db96d56Sopenharmony_ci      sent prior to execution of the CGI script.  This pre-empts the status
4717db96d56Sopenharmony_ci      code.
4727db96d56Sopenharmony_ci
4737db96d56Sopenharmony_ci   The class will however, run the CGI script, instead of serving it as a file,
4747db96d56Sopenharmony_ci   if it guesses it to be a CGI script.  Only directory-based CGI are used ---
4757db96d56Sopenharmony_ci   the other common server configuration is to treat special extensions as
4767db96d56Sopenharmony_ci   denoting CGI scripts.
4777db96d56Sopenharmony_ci
4787db96d56Sopenharmony_ci   The :func:`do_GET` and :func:`do_HEAD` functions are modified to run CGI scripts
4797db96d56Sopenharmony_ci   and serve the output, instead of serving files, if the request leads to
4807db96d56Sopenharmony_ci   somewhere below the ``cgi_directories`` path.
4817db96d56Sopenharmony_ci
4827db96d56Sopenharmony_ci   The :class:`CGIHTTPRequestHandler` defines the following data member:
4837db96d56Sopenharmony_ci
4847db96d56Sopenharmony_ci   .. attribute:: cgi_directories
4857db96d56Sopenharmony_ci
4867db96d56Sopenharmony_ci      This defaults to ``['/cgi-bin', '/htbin']`` and describes directories to
4877db96d56Sopenharmony_ci      treat as containing CGI scripts.
4887db96d56Sopenharmony_ci
4897db96d56Sopenharmony_ci   The :class:`CGIHTTPRequestHandler` defines the following method:
4907db96d56Sopenharmony_ci
4917db96d56Sopenharmony_ci   .. method:: do_POST()
4927db96d56Sopenharmony_ci
4937db96d56Sopenharmony_ci      This method serves the ``'POST'`` request type, only allowed for CGI
4947db96d56Sopenharmony_ci      scripts.  Error 501, "Can only POST to CGI scripts", is output when trying
4957db96d56Sopenharmony_ci      to POST to a non-CGI url.
4967db96d56Sopenharmony_ci
4977db96d56Sopenharmony_ci   Note that CGI scripts will be run with UID of user nobody, for security
4987db96d56Sopenharmony_ci   reasons.  Problems with the CGI script will be translated to error 403.
4997db96d56Sopenharmony_ci
5007db96d56Sopenharmony_ci:class:`CGIHTTPRequestHandler` can be enabled in the command line by passing
5017db96d56Sopenharmony_cithe ``--cgi`` option::
5027db96d56Sopenharmony_ci
5037db96d56Sopenharmony_ci        python -m http.server --cgi
5047db96d56Sopenharmony_ci
5057db96d56Sopenharmony_ci.. _http.server-security:
5067db96d56Sopenharmony_ci
5077db96d56Sopenharmony_ciSecurity Considerations
5087db96d56Sopenharmony_ci-----------------------
5097db96d56Sopenharmony_ci
5107db96d56Sopenharmony_ci.. index:: pair: http.server; security
5117db96d56Sopenharmony_ci
5127db96d56Sopenharmony_ci:class:`SimpleHTTPRequestHandler` will follow symbolic links when handling
5137db96d56Sopenharmony_cirequests, this makes it possible for files outside of the specified directory
5147db96d56Sopenharmony_cito be served.
5157db96d56Sopenharmony_ci
5167db96d56Sopenharmony_ciEarlier versions of Python did not scrub control characters from the
5177db96d56Sopenharmony_cilog messages emitted to stderr from ``python -m http.server`` or the
5187db96d56Sopenharmony_cidefault :class:`BaseHTTPRequestHandler` ``.log_message``
5197db96d56Sopenharmony_ciimplementation. This could allow remote clients connecting to your
5207db96d56Sopenharmony_ciserver to send nefarious control codes to your terminal.
5217db96d56Sopenharmony_ci
5227db96d56Sopenharmony_ci.. versionadded:: 3.11.1
5237db96d56Sopenharmony_ci   Control characters are scrubbed in stderr logs.
524