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