Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | :mod:`BaseHTTPServer` --- Basic HTTP server |
| 2 | =========================================== |
| 3 | |
| 4 | .. module:: BaseHTTPServer |
| 5 | :synopsis: Basic HTTP server (base class for SimpleHTTPServer and CGIHTTPServer). |
| 6 | |
Georg Brandl | 8de9119 | 2008-05-26 15:01:48 +0000 | [diff] [blame] | 7 | .. note:: |
| 8 | The :mod:`BaseHTTPServer` module has been merged into :mod:`http.server` in |
| 9 | Python 3.0. The :term:`2to3` tool will automatically adapt imports when |
| 10 | converting your sources to 3.0. |
| 11 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 12 | |
| 13 | .. index:: |
| 14 | pair: WWW; server |
| 15 | pair: HTTP; protocol |
| 16 | single: URL |
| 17 | single: httpd |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 18 | module: SimpleHTTPServer |
| 19 | module: CGIHTTPServer |
| 20 | |
Éric Araujo | 29a0b57 | 2011-08-19 02:14:03 +0200 | [diff] [blame] | 21 | **Source code:** :source:`Lib/BaseHTTPServer.py` |
| 22 | |
| 23 | -------------- |
| 24 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 25 | This module defines two classes for implementing HTTP servers (Web servers). |
| 26 | Usually, this module isn't used directly, but is used as a basis for building |
| 27 | functioning Web servers. See the :mod:`SimpleHTTPServer` and |
| 28 | :mod:`CGIHTTPServer` modules. |
| 29 | |
Georg Brandl | e152a77 | 2008-05-24 18:31:28 +0000 | [diff] [blame] | 30 | The first class, :class:`HTTPServer`, is a :class:`SocketServer.TCPServer` |
Georg Brandl | 9af0c56 | 2009-04-05 10:24:20 +0000 | [diff] [blame] | 31 | subclass, and therefore implements the :class:`SocketServer.BaseServer` |
| 32 | interface. It creates and listens at the HTTP socket, dispatching the requests |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 33 | to a handler. Code to create and run the server looks like this:: |
| 34 | |
| 35 | def run(server_class=BaseHTTPServer.HTTPServer, |
| 36 | handler_class=BaseHTTPServer.BaseHTTPRequestHandler): |
| 37 | server_address = ('', 8000) |
| 38 | httpd = server_class(server_address, handler_class) |
| 39 | httpd.serve_forever() |
| 40 | |
| 41 | |
| 42 | .. class:: HTTPServer(server_address, RequestHandlerClass) |
| 43 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 44 | This class builds on the :class:`TCPServer` class by storing the server |
| 45 | address as instance variables named :attr:`server_name` and |
| 46 | :attr:`server_port`. The server is accessible by the handler, typically |
| 47 | through the handler's :attr:`server` instance variable. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 48 | |
| 49 | |
| 50 | .. class:: BaseHTTPRequestHandler(request, client_address, server) |
| 51 | |
| 52 | This class is used to handle the HTTP requests that arrive at the server. By |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 53 | itself, it cannot respond to any actual HTTP requests; it must be subclassed |
| 54 | to handle each request method (e.g. GET or |
| 55 | POST). :class:`BaseHTTPRequestHandler` provides a number of class and |
| 56 | instance variables, and methods for use by subclasses. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 57 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 58 | The handler will parse the request and the headers, then call a method |
| 59 | specific to the request type. The method name is constructed from the |
| 60 | request. For example, for the request method ``SPAM``, the :meth:`do_SPAM` |
| 61 | method will be called with no arguments. All of the relevant information is |
| 62 | stored in instance variables of the handler. Subclasses should not need to |
| 63 | override or extend the :meth:`__init__` method. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 64 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 65 | :class:`BaseHTTPRequestHandler` has the following instance variables: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 66 | |
| 67 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 68 | .. attribute:: client_address |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 69 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 70 | Contains a tuple of the form ``(host, port)`` referring to the client's |
| 71 | address. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 72 | |
| 73 | |
Georg Brandl | cff0b46 | 2008-08-30 09:49:36 +0000 | [diff] [blame] | 74 | .. attribute:: server |
| 75 | |
| 76 | Contains the server instance. |
| 77 | |
| 78 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 79 | .. attribute:: command |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 80 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 81 | Contains the command (request type). For example, ``'GET'``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 82 | |
| 83 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 84 | .. attribute:: path |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 85 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 86 | Contains the request path. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 87 | |
| 88 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 89 | .. attribute:: request_version |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 90 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 91 | Contains the version string from the request. For example, ``'HTTP/1.0'``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 92 | |
| 93 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 94 | .. attribute:: headers |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 95 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 96 | Holds an instance of the class specified by the :attr:`MessageClass` class |
| 97 | variable. This instance parses and manages the headers in the HTTP |
| 98 | request. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 99 | |
| 100 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 101 | .. attribute:: rfile |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 102 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 103 | Contains an input stream, positioned at the start of the optional input |
| 104 | data. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 105 | |
| 106 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 107 | .. attribute:: wfile |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 108 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 109 | Contains the output stream for writing a response back to the |
| 110 | client. Proper adherence to the HTTP protocol must be used when writing to |
| 111 | this stream. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 112 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 113 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 114 | :class:`BaseHTTPRequestHandler` has the following class variables: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 115 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 116 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 117 | .. attribute:: server_version |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 118 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 119 | Specifies the server software version. You may want to override this. The |
| 120 | format is multiple whitespace-separated strings, where each string is of |
| 121 | the form name[/version]. For example, ``'BaseHTTP/0.2'``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 122 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 123 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 124 | .. attribute:: sys_version |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 125 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 126 | Contains the Python system version, in a form usable by the |
| 127 | :attr:`version_string` method and the :attr:`server_version` class |
| 128 | variable. For example, ``'Python/1.4'``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 129 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 130 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 131 | .. attribute:: error_message_format |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 132 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 133 | Specifies a format string for building an error response to the client. It |
| 134 | uses parenthesized, keyed format specifiers, so the format operand must be |
| 135 | a dictionary. The *code* key should be an integer, specifying the numeric |
| 136 | HTTP error code value. *message* should be a string containing a |
| 137 | (detailed) error message of what occurred, and *explain* should be an |
| 138 | explanation of the error code number. Default *message* and *explain* |
| 139 | values can found in the *responses* class variable. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 140 | |
Georg Brandl | 1647923 | 2008-02-23 15:02:28 +0000 | [diff] [blame] | 141 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 142 | .. attribute:: error_content_type |
Georg Brandl | 1647923 | 2008-02-23 15:02:28 +0000 | [diff] [blame] | 143 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 144 | Specifies the Content-Type HTTP header of error responses sent to the |
| 145 | client. The default value is ``'text/html'``. |
Georg Brandl | 1647923 | 2008-02-23 15:02:28 +0000 | [diff] [blame] | 146 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 147 | .. versionadded:: 2.6 |
| 148 | Previously, the content type was always ``'text/html'``. |
Georg Brandl | 1647923 | 2008-02-23 15:02:28 +0000 | [diff] [blame] | 149 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 150 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 151 | .. attribute:: protocol_version |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 152 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 153 | This specifies the HTTP protocol version used in responses. If set to |
| 154 | ``'HTTP/1.1'``, the server will permit HTTP persistent connections; |
| 155 | however, your server *must* then include an accurate ``Content-Length`` |
| 156 | header (using :meth:`send_header`) in all of its responses to clients. |
| 157 | For backwards compatibility, the setting defaults to ``'HTTP/1.0'``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 158 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 159 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 160 | .. attribute:: MessageClass |
| 161 | |
| 162 | .. index:: single: Message (in module mimetools) |
| 163 | |
| 164 | Specifies a :class:`rfc822.Message`\ -like class to parse HTTP headers. |
| 165 | Typically, this is not overridden, and it defaults to |
| 166 | :class:`mimetools.Message`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 167 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 168 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 169 | .. attribute:: responses |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 170 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 171 | This variable contains a mapping of error code integers to two-element tuples |
| 172 | containing a short and long message. For example, ``{code: (shortmessage, |
| 173 | longmessage)}``. The *shortmessage* is usually used as the *message* key in an |
| 174 | error response, and *longmessage* as the *explain* key (see the |
| 175 | :attr:`error_message_format` class variable). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 176 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 177 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 178 | A :class:`BaseHTTPRequestHandler` instance has the following methods: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 179 | |
| 180 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 181 | .. method:: handle() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 182 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 183 | Calls :meth:`handle_one_request` once (or, if persistent connections are |
| 184 | enabled, multiple times) to handle incoming HTTP requests. You should |
| 185 | never need to override it; instead, implement appropriate :meth:`do_\*` |
| 186 | methods. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 187 | |
| 188 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 189 | .. method:: handle_one_request() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 190 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 191 | This method will parse and dispatch the request to the appropriate |
| 192 | :meth:`do_\*` method. You should never need to override it. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 193 | |
| 194 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 195 | .. method:: send_error(code[, message]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 196 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 197 | Sends and logs a complete error reply to the client. The numeric *code* |
| 198 | specifies the HTTP error code, with *message* as optional, more specific text. A |
| 199 | complete set of headers is sent, followed by text composed using the |
| 200 | :attr:`error_message_format` class variable. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 201 | |
| 202 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 203 | .. method:: send_response(code[, message]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 204 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 205 | Sends a response header and logs the accepted request. The HTTP response |
| 206 | line is sent, followed by *Server* and *Date* headers. The values for |
| 207 | these two headers are picked up from the :meth:`version_string` and |
| 208 | :meth:`date_time_string` methods, respectively. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 209 | |
| 210 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 211 | .. method:: send_header(keyword, value) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 212 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 213 | Writes a specific HTTP header to the output stream. *keyword* should |
| 214 | specify the header keyword, with *value* specifying its value. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 215 | |
| 216 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 217 | .. method:: end_headers() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 218 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 219 | Sends a blank line, indicating the end of the HTTP headers in the |
| 220 | response. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 221 | |
| 222 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 223 | .. method:: log_request([code[, size]]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 224 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 225 | Logs an accepted (successful) request. *code* should specify the numeric |
| 226 | HTTP code associated with the response. If a size of the response is |
| 227 | available, then it should be passed as the *size* parameter. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 228 | |
| 229 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 230 | .. method:: log_error(...) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 231 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 232 | Logs an error when a request cannot be fulfilled. By default, it passes |
| 233 | the message to :meth:`log_message`, so it takes the same arguments |
| 234 | (*format* and additional values). |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 235 | |
| 236 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 237 | .. method:: log_message(format, ...) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 238 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 239 | Logs an arbitrary message to ``sys.stderr``. This is typically overridden |
| 240 | to create custom error logging mechanisms. The *format* argument is a |
| 241 | standard printf-style format string, where the additional arguments to |
| 242 | :meth:`log_message` are applied as inputs to the formatting. The client |
Senthil Kumaran | fb5aebc | 2012-04-29 13:39:16 +0800 | [diff] [blame^] | 243 | ip address and current date and time are prefixed to every message logged. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 244 | |
| 245 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 246 | .. method:: version_string() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 247 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 248 | Returns the server software's version string. This is a combination of the |
| 249 | :attr:`server_version` and :attr:`sys_version` class variables. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 250 | |
| 251 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 252 | .. method:: date_time_string([timestamp]) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 253 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 254 | Returns the date and time given by *timestamp* (which must be in the |
| 255 | format returned by :func:`time.time`), formatted for a message header. If |
| 256 | *timestamp* is omitted, it uses the current date and time. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 257 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 258 | The result looks like ``'Sun, 06 Nov 1994 08:49:37 GMT'``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 259 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 260 | .. versionadded:: 2.5 |
| 261 | The *timestamp* parameter. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 262 | |
| 263 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 264 | .. method:: log_date_time_string() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 265 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 266 | Returns the current date and time, formatted for logging. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 267 | |
| 268 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 269 | .. method:: address_string() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 270 | |
Benjamin Peterson | c7b0592 | 2008-04-25 01:29:10 +0000 | [diff] [blame] | 271 | Returns the client address, formatted for logging. A name lookup is |
| 272 | performed on the client's IP address. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 273 | |
| 274 | |
Georg Brandl | 9af0c56 | 2009-04-05 10:24:20 +0000 | [diff] [blame] | 275 | More examples |
| 276 | ------------- |
| 277 | |
| 278 | To create a server that doesn't run forever, but until some condition is |
| 279 | fulfilled:: |
| 280 | |
| 281 | def run_while_true(server_class=BaseHTTPServer.HTTPServer, |
| 282 | handler_class=BaseHTTPServer.BaseHTTPRequestHandler): |
| 283 | """ |
| 284 | This assumes that keep_running() is a function of no arguments which |
| 285 | is tested initially and after each request. If its return value |
| 286 | is true, the server continues. |
| 287 | """ |
| 288 | server_address = ('', 8000) |
| 289 | httpd = server_class(server_address, handler_class) |
| 290 | while keep_running(): |
| 291 | httpd.handle_request() |
| 292 | |
| 293 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 294 | .. seealso:: |
| 295 | |
| 296 | Module :mod:`CGIHTTPServer` |
| 297 | Extended request handler that supports CGI scripts. |
| 298 | |
| 299 | Module :mod:`SimpleHTTPServer` |
Georg Brandl | 9af0c56 | 2009-04-05 10:24:20 +0000 | [diff] [blame] | 300 | Basic request handler that limits response to files actually under the |
| 301 | document root. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 302 | |