You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
4.3 KiB

16 years ago
  1. :mod:`SimpleHTTPServer` --- Simple HTTP request handler
  2. =======================================================
  3. .. module:: SimpleHTTPServer
  4. :synopsis: This module provides a basic request handler for HTTP servers.
  5. .. sectionauthor:: Moshe Zadka <moshez@zadka.site.co.il>
  6. .. note::
  7. The :mod:`SimpleHTTPServer` module has been merged into :mod:`http.server` in
  8. Python 3. The :term:`2to3` tool will automatically adapt imports when
  9. converting your sources to Python 3.
  10. The :mod:`SimpleHTTPServer` module defines a single class,
  11. :class:`SimpleHTTPRequestHandler`, which is interface-compatible with
  12. :class:`BaseHTTPServer.BaseHTTPRequestHandler`.
  13. The :mod:`SimpleHTTPServer` module defines the following class:
  14. .. class:: SimpleHTTPRequestHandler(request, client_address, server)
  15. This class serves files from the current directory and below, directly
  16. mapping the directory structure to HTTP requests.
  17. A lot of the work, such as parsing the request, is done by the base class
  18. :class:`BaseHTTPServer.BaseHTTPRequestHandler`. This class implements the
  19. :func:`do_GET` and :func:`do_HEAD` functions.
  20. The following are defined as class-level attributes of
  21. :class:`SimpleHTTPRequestHandler`:
  22. .. attribute:: server_version
  23. This will be ``"SimpleHTTP/" + __version__``, where ``__version__`` is
  24. defined at the module level.
  25. .. attribute:: extensions_map
  26. A dictionary mapping suffixes into MIME types. The default is
  27. signified by an empty string, and is considered to be
  28. ``application/octet-stream``. The mapping is used case-insensitively,
  29. and so should contain only lower-cased keys.
  30. The :class:`SimpleHTTPRequestHandler` class defines the following methods:
  31. .. method:: do_HEAD()
  32. This method serves the ``'HEAD'`` request type: it sends the headers it
  33. would send for the equivalent ``GET`` request. See the :meth:`do_GET`
  34. method for a more complete explanation of the possible headers.
  35. .. method:: do_GET()
  36. The request is mapped to a local file by interpreting the request as a
  37. path relative to the current working directory.
  38. If the request was mapped to a directory, the directory is checked for a
  39. file named ``index.html`` or ``index.htm`` (in that order). If found, the
  40. file's contents are returned; otherwise a directory listing is generated
  41. by calling the :meth:`list_directory` method. This method uses
  42. :func:`os.listdir` to scan the directory, and returns a ``404`` error
  43. response if the :func:`listdir` fails.
  44. If the request was mapped to a file, it is opened and the contents are
  45. returned. Any :exc:`IOError` exception in opening the requested file is
  46. mapped to a ``404``, ``'File not found'`` error. Otherwise, the content
  47. type is guessed by calling the :meth:`guess_type` method, which in turn
  48. uses the *extensions_map* variable.
  49. A ``'Content-type:'`` header with the guessed content type is output,
  50. followed by a ``'Content-Length:'`` header with the file's size and a
  51. ``'Last-Modified:'`` header with the file's modification time.
  52. Then follows a blank line signifying the end of the headers, and then the
  53. contents of the file are output. If the file's MIME type starts with
  54. ``text/`` the file is opened in text mode; otherwise binary mode is used.
  55. The :func:`test` function in the :mod:`SimpleHTTPServer` module is an
  56. example which creates a server using the :class:`SimpleHTTPRequestHandler`
  57. as the Handler.
  58. .. versionadded:: 2.5
  59. The ``'Last-Modified'`` header.
  60. The :mod:`SimpleHTTPServer` module can be used in the following manner in order
  61. to set up a very basic web server serving files relative to the current
  62. directory. ::
  63. import SimpleHTTPServer
  64. import SocketServer
  65. PORT = 8000
  66. Handler = SimpleHTTPServer.SimpleHTTPRequestHandler
  67. httpd = SocketServer.TCPServer(("", PORT), Handler)
  68. print "serving at port", PORT
  69. httpd.serve_forever()
  70. The :mod:`SimpleHTTPServer` module can also be invoked directly using the
  71. :option:`-m` switch of the interpreter with a ``port number`` argument.
  72. Similar to the previous example, this serves the files relative to the
  73. current directory. ::
  74. python -m SimpleHTTPServer 8000
  75. .. seealso::
  76. Module :mod:`BaseHTTPServer`
  77. Base class implementation for Web server and request handler.