Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 1 | \section{Standard Module \sectcode{BaseHTTPServer}} |
| 2 | \label{module-BaseHTTPServer} |
| 3 | \stmodindex{BaseHTTPServer} |
| 4 | |
| 5 | \indexii{WWW}{server} |
| 6 | \indexii{HTTP}{protocol} |
| 7 | \index{URL} |
| 8 | \index{httpd} |
| 9 | |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 10 | |
| 11 | This module defines two classes for implementing HTTP servers |
| 12 | (web servers). Usually, this module isn't used directly, but is used |
| 13 | as a basis for building functioning web servers. See the |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 14 | \module{SimpleHTTPServer} and \module{CGIHTTPServer} modules. |
| 15 | \refstmodindex{SimpleHTTPServer} |
| 16 | \refstmodindex{CGIHTTPServer} |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 17 | |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 18 | The first class, \class{HTTPServer}, is a |
| 19 | \class{SocketServer.TCPServer} subclass. It creates and listens at the |
| 20 | web socket, dispatching the requests to a handler. Code to create and |
| 21 | run the server looks like this: |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 22 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 23 | \begin{verbatim} |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 24 | def run(server_class=BaseHTTPServer.HTTPServer, |
| 25 | handler_class=BaseHTTPServer.BaseHTTPRequestHandler): |
| 26 | server_address = ('', 8000) |
| 27 | httpd = server_class(server_address, handler_class) |
| 28 | httpd.serve_forever() |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 29 | \end{verbatim} |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 30 | |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 31 | The \class{HTTPServer} class builds on the \class{TCPServer} class by |
| 32 | storing the server address as instance |
| 33 | variables named \member{server_name} and \member{server_port}. The |
| 34 | server is accessible by the handler, typically through the handler's |
| 35 | \member{server} instance variable. |
| 36 | |
| 37 | The module's second class, \class{BaseHTTPRequestHandler}, is used |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 38 | to handle the HTTP requests that arrive at the server. By itself, |
| 39 | it cannot respond to any actual HTTP requests; it must be subclassed |
| 40 | to handle each request method (e.g. GET or POST). |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 41 | \class{BaseHTTPRequestHandler} provides a number of class and instance |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 42 | variables, and methods for use by subclasses. |
| 43 | |
| 44 | The handler will parse the request and the headers, then call a |
| 45 | method specific to the request type. The method name is constructed |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 46 | from the request. For example, for the request \samp{SPAM}, the |
| 47 | \method{do_SPAM()} method will be called with no arguments. All of |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 48 | the relevant information is stored into instance variables of the |
| 49 | handler. |
| 50 | |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 51 | \setindexsubitem{(BaseHTTPRequestHandler attribute)} |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 52 | |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 53 | \class{BaseHTTPRequestHandler} has the following instance variables: |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 54 | |
| 55 | \begin{datadesc}{client_address} |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 56 | Contains a tuple of the form \code{(\var{host}, \var{port})} referring |
| 57 | to the client's address. |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 58 | \end{datadesc} |
| 59 | |
| 60 | \begin{datadesc}{command} |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 61 | Contains the command (request type). For example, \code{'GET'}. |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 62 | \end{datadesc} |
| 63 | |
| 64 | \begin{datadesc}{path} |
| 65 | Contains the request path. |
| 66 | \end{datadesc} |
| 67 | |
| 68 | \begin{datadesc}{request_version} |
| 69 | Contains the version string from the request. For example, |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 70 | \code{'HTTP/1.0'}. |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 71 | \end{datadesc} |
| 72 | |
| 73 | \begin{datadesc}{headers} |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 74 | Holds an instance of the class specified by the \member{MessageClass} |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 75 | class variable. This instance parses and manages the headers in |
| 76 | the HTTP request. |
| 77 | \end{datadesc} |
| 78 | |
| 79 | \begin{datadesc}{rfile} |
| 80 | Contains an input stream, positioned at the start of the optional |
| 81 | input data. |
| 82 | \end{datadesc} |
| 83 | |
| 84 | \begin{datadesc}{wfile} |
| 85 | Contains the output stream for writing a response back to the client. |
| 86 | Proper adherance to the HTTP protocol must be used when writing |
| 87 | to this stream. |
| 88 | \end{datadesc} |
| 89 | |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 90 | \setindexsubitem{(BaseHTTPRequestHandler attribute)} |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 91 | |
| 92 | \code{BaseHTTPRequestHandler} has the following class variables: |
| 93 | |
| 94 | \begin{datadesc}{server_version} |
| 95 | Specifies the server software version. You may want to override |
| 96 | this. |
| 97 | The format is multiple whitespace-separated strings, |
| 98 | where each string is of the form name[/version]. |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 99 | For example, \code{'BaseHTTP/0.2'}. |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 100 | \end{datadesc} |
| 101 | |
| 102 | \begin{datadesc}{sys_version} |
| 103 | Contains the Python system version, in a form usable by the |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 104 | \member{version_string} method and the \member{server_version} class |
| 105 | variable. For example, \code{'Python/1.4'}. |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 106 | \end{datadesc} |
| 107 | |
| 108 | \begin{datadesc}{error_message_format} |
| 109 | Specifies a format string for building an error response to the |
| 110 | client. It uses parenthesized, keyed format specifiers, so the |
| 111 | format operand must be a dictionary. The \var{code} key should |
| 112 | be an integer, specifing the numeric HTTP error code value. |
| 113 | \var{message} should be a string containing a (detailed) error |
| 114 | message of what occurred, and \var{explain} should be an |
| 115 | explanation of the error code number. Default \var{message} |
| 116 | and \var{explain} values can found in the \var{responses} |
| 117 | class variable. |
| 118 | \end{datadesc} |
| 119 | |
| 120 | \begin{datadesc}{protocol_version} |
| 121 | This specifies the HTTP protocol version used in responses. |
| 122 | Typically, this should not be overridden. Defaults to |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 123 | \code{'HTTP/1.0'}. |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 124 | \end{datadesc} |
| 125 | |
| 126 | \begin{datadesc}{MessageClass} |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 127 | Specifies a \class{rfc822.Message}-like class to parse HTTP |
| 128 | headers. Typically, this is not overridden, and it defaults to |
| 129 | \class{mimetools.Message}. |
| 130 | \withsubitem{(in module mimetools)}{\ttindex{Message}} |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 131 | \end{datadesc} |
| 132 | |
| 133 | \begin{datadesc}{responses} |
| 134 | This variable contains a mapping of error code integers to two-element |
| 135 | tuples containing a short and long message. For example, |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 136 | \code{\{\var{code}: (\var{shortmessage}, \var{longmessage})\}}. The |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 137 | \var{shortmessage} is usually used as the \var{message} key in an |
| 138 | error response, and \var{longmessage} as the \var{explain} key |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 139 | (see the \member{error_message_format} class variable). |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 140 | \end{datadesc} |
| 141 | |
Fred Drake | 1947991 | 1998-02-13 06:58:54 +0000 | [diff] [blame] | 142 | \setindexsubitem{(BaseHTTPRequestHandler method)} |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 143 | |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 144 | A \class{BaseHTTPRequestHandler} instance has the following methods: |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 145 | |
| 146 | \begin{funcdesc}{handle}{} |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 147 | Overrides the superclass' \method{handle()} method to provide the |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 148 | specific handler behavior. This method will parse and dispatch |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 149 | the request to the appropriate \code{do_*()} method. |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 150 | \end{funcdesc} |
| 151 | |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 152 | \begin{funcdesc}{send_error}{code\optional{, message}} |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 153 | Sends and logs a complete error reply to the client. The numeric |
| 154 | \var{code} specifies the HTTP error code, with \var{message} as |
| 155 | optional, more specific text. A complete set of headers is sent, |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 156 | followed by text composed using the \member{error_message_format} |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 157 | class variable. |
| 158 | \end{funcdesc} |
| 159 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 160 | \begin{funcdesc}{send_response}{code\optional{, message}} |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 161 | Sends a response header and logs the accepted request. The HTTP |
| 162 | response line is sent, followed by \emph{Server} and \emph{Date} |
| 163 | headers. The values for these two headers are picked up from the |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 164 | \method{version_string()} and \method{date_time_string()} methods, |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 165 | respectively. |
| 166 | \end{funcdesc} |
| 167 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 168 | \begin{funcdesc}{send_header}{keyword, value} |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 169 | Writes a specific MIME header to the output stream. \var{keyword} |
| 170 | should specify the header keyword, with \var{value} specifying |
| 171 | its value. |
| 172 | \end{funcdesc} |
| 173 | |
| 174 | \begin{funcdesc}{end_headers}{} |
| 175 | Sends a blank line, indicating the end of the MIME headers in |
| 176 | the response. |
| 177 | \end{funcdesc} |
| 178 | |
Fred Drake | cce1090 | 1998-03-17 06:33:25 +0000 | [diff] [blame] | 179 | \begin{funcdesc}{log_request}{\optional{code\optional{, size}}} |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 180 | Logs an accepted (successful) request. \var{code} should specify |
| 181 | the numeric HTTP code associated with the response. If a size of |
| 182 | the response is available, then it should be passed as the |
| 183 | \var{size} parameter. |
| 184 | \end{funcdesc} |
| 185 | |
| 186 | \begin{funcdesc}{log_error}{...} |
| 187 | Logs an error when a request cannot be fulfilled. By default, |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 188 | it passes the message to \method{log_message()}, so it takes the |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 189 | same arguments (\var{format} and additional values). |
| 190 | \end{funcdesc} |
| 191 | |
| 192 | \begin{funcdesc}{log_message}{format, ...} |
| 193 | Logs an arbitrary message to \code{sys.stderr}. This is typically |
| 194 | overridden to create custom error logging mechanisms. The |
| 195 | \var{format} argument is a standard printf-style format string, |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 196 | where the additional arguments to \method{log_message()} are applied |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 197 | as inputs to the formatting. The client address and current date |
| 198 | and time are prefixed to every message logged. |
| 199 | \end{funcdesc} |
| 200 | |
| 201 | \begin{funcdesc}{version_string}{} |
| 202 | Returns the server software's version string. This is a combination |
Fred Drake | f9e1f65 | 1998-03-14 07:00:41 +0000 | [diff] [blame] | 203 | of the \member{server_version} and \member{sys_version} class variables. |
Guido van Rossum | 9cb6480 | 1997-12-29 20:01:55 +0000 | [diff] [blame] | 204 | \end{funcdesc} |
| 205 | |
| 206 | \begin{funcdesc}{date_time_string}{} |
| 207 | Returns the current date and time, formatted for a message header. |
| 208 | \end{funcdesc} |
| 209 | |
| 210 | \begin{funcdesc}{log_data_time_string}{} |
| 211 | Returns the current date and time, formatted for logging. |
| 212 | \end{funcdesc} |
| 213 | |
| 214 | \begin{funcdesc}{address_string}{} |
| 215 | Returns the client address, formatted for logging. A name lookup |
| 216 | is performed on the client's IP address. |
| 217 | \end{funcdesc} |