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