blob: fe794f87a4e662fca9b0e1203a8d69bdabdfed3c [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{BaseHTTPServer} ---
Fred Drake4d3714b2000-10-10 16:56:41 +00002 Basic HTTP server}
Fred Drakeb91e9341998-07-23 17:59:49 +00003
Fred Drake4d3714b2000-10-10 16:56:41 +00004\declaremodule{standard}{BaseHTTPServer}
Fred Drake72d157e1998-08-06 21:23:17 +00005\modulesynopsis{Basic HTTP server (base class for
Fred Drake4d3714b2000-10-10 16:56:41 +00006 \class{SimpleHTTPServer} and \class{CGIHTTPServer}).}
Fred Drakeb91e9341998-07-23 17:59:49 +00007
Guido van Rossum9cb64801997-12-29 20:01:55 +00008
9\indexii{WWW}{server}
10\indexii{HTTP}{protocol}
11\index{URL}
12\index{httpd}
13
Guido van Rossum9cb64801997-12-29 20:01:55 +000014This module defines two classes for implementing HTTP servers
Fred Drake8ee679f2001-07-14 02:50:55 +000015(Web servers). Usually, this module isn't used directly, but is used
16as a basis for building functioning Web servers. See the
Fred Drakeda4513a2004-06-09 14:50:19 +000017\refmodule{SimpleHTTPServer}\refstmodindex{SimpleHTTPServer} and
Fred Drakec46864e1999-06-14 19:49:50 +000018\refmodule{CGIHTTPServer}\refstmodindex{CGIHTTPServer} modules.
Guido van Rossum9cb64801997-12-29 20:01:55 +000019
Fred Drakef9e1f651998-03-14 07:00:41 +000020The first class, \class{HTTPServer}, is a
Fred Drake8ee679f2001-07-14 02:50:55 +000021\class{SocketServer.TCPServer} subclass. It creates and listens at the
22HTTP socket, dispatching the requests to a handler. Code to create and
Fred Drakef9e1f651998-03-14 07:00:41 +000023run the server looks like this:
Guido van Rossum9cb64801997-12-29 20:01:55 +000024
Fred Drake19479911998-02-13 06:58:54 +000025\begin{verbatim}
Guido van Rossum9cb64801997-12-29 20:01:55 +000026def run(server_class=BaseHTTPServer.HTTPServer,
27 handler_class=BaseHTTPServer.BaseHTTPRequestHandler):
Fred Drakefc576191998-04-04 07:15:02 +000028 server_address = ('', 8000)
29 httpd = server_class(server_address, handler_class)
30 httpd.serve_forever()
Fred Drake19479911998-02-13 06:58:54 +000031\end{verbatim}
Guido van Rossum9cb64801997-12-29 20:01:55 +000032
Fred Drakefc576191998-04-04 07:15:02 +000033\begin{classdesc}{HTTPServer}{server_address, RequestHandlerClass}
34This class builds on the \class{TCPServer} class by
Fred Drakef9e1f651998-03-14 07:00:41 +000035storing the server address as instance
36variables named \member{server_name} and \member{server_port}. The
37server is accessible by the handler, typically through the handler's
38\member{server} instance variable.
Fred Drakefc576191998-04-04 07:15:02 +000039\end{classdesc}
Fred Drakef9e1f651998-03-14 07:00:41 +000040
Fred Drakefc576191998-04-04 07:15:02 +000041\begin{classdesc}{BaseHTTPRequestHandler}{request, client_address, server}
42This class is used
Guido van Rossum9cb64801997-12-29 20:01:55 +000043to handle the HTTP requests that arrive at the server. By itself,
44it cannot respond to any actual HTTP requests; it must be subclassed
45to handle each request method (e.g. GET or POST).
Fred Drakef9e1f651998-03-14 07:00:41 +000046\class{BaseHTTPRequestHandler} provides a number of class and instance
Guido van Rossum9cb64801997-12-29 20:01:55 +000047variables, and methods for use by subclasses.
48
49The handler will parse the request and the headers, then call a
50method specific to the request type. The method name is constructed
Fred Drakefc576191998-04-04 07:15:02 +000051from the request. For example, for the request method \samp{SPAM}, the
Fred Drakef9e1f651998-03-14 07:00:41 +000052\method{do_SPAM()} method will be called with no arguments. All of
Fred Drakefc576191998-04-04 07:15:02 +000053the relevant information is stored in instance variables of the
54handler. Subclasses should not need to override or extend the
55\method{__init__()} method.
56\end{classdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000057
Guido van Rossum9cb64801997-12-29 20:01:55 +000058
Fred Drakef9e1f651998-03-14 07:00:41 +000059\class{BaseHTTPRequestHandler} has the following instance variables:
Guido van Rossum9cb64801997-12-29 20:01:55 +000060
Fred Drakefc576191998-04-04 07:15:02 +000061\begin{memberdesc}{client_address}
Fred Drakef9e1f651998-03-14 07:00:41 +000062Contains a tuple of the form \code{(\var{host}, \var{port})} referring
63to the client's address.
Fred Drakefc576191998-04-04 07:15:02 +000064\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000065
Fred Drakefc576191998-04-04 07:15:02 +000066\begin{memberdesc}{command}
Fred Drakef9e1f651998-03-14 07:00:41 +000067Contains the command (request type). For example, \code{'GET'}.
Fred Drakefc576191998-04-04 07:15:02 +000068\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000069
Fred Drakefc576191998-04-04 07:15:02 +000070\begin{memberdesc}{path}
Guido van Rossum9cb64801997-12-29 20:01:55 +000071Contains the request path.
Fred Drakefc576191998-04-04 07:15:02 +000072\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000073
Fred Drakefc576191998-04-04 07:15:02 +000074\begin{memberdesc}{request_version}
Guido van Rossum9cb64801997-12-29 20:01:55 +000075Contains the version string from the request. For example,
Fred Drakef9e1f651998-03-14 07:00:41 +000076\code{'HTTP/1.0'}.
Fred Drakefc576191998-04-04 07:15:02 +000077\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000078
Fred Drakefc576191998-04-04 07:15:02 +000079\begin{memberdesc}{headers}
Fred Drakef9e1f651998-03-14 07:00:41 +000080Holds an instance of the class specified by the \member{MessageClass}
Guido van Rossum9cb64801997-12-29 20:01:55 +000081class variable. This instance parses and manages the headers in
82the HTTP request.
Fred Drakefc576191998-04-04 07:15:02 +000083\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000084
Fred Drakefc576191998-04-04 07:15:02 +000085\begin{memberdesc}{rfile}
Guido van Rossum9cb64801997-12-29 20:01:55 +000086Contains an input stream, positioned at the start of the optional
87input data.
Fred Drakefc576191998-04-04 07:15:02 +000088\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000089
Fred Drakefc576191998-04-04 07:15:02 +000090\begin{memberdesc}{wfile}
Guido van Rossum9cb64801997-12-29 20:01:55 +000091Contains the output stream for writing a response back to the client.
Thomas Woutersf8316632000-07-16 19:01:10 +000092Proper adherence to the HTTP protocol must be used when writing
Guido van Rossum9cb64801997-12-29 20:01:55 +000093to this stream.
Fred Drakefc576191998-04-04 07:15:02 +000094\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000095
Guido van Rossum9cb64801997-12-29 20:01:55 +000096
Fred Drakefc576191998-04-04 07:15:02 +000097\class{BaseHTTPRequestHandler} has the following class variables:
Guido van Rossum9cb64801997-12-29 20:01:55 +000098
Fred Drakefc576191998-04-04 07:15:02 +000099\begin{memberdesc}{server_version}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000100Specifies the server software version. You may want to override
101this.
102The format is multiple whitespace-separated strings,
103where each string is of the form name[/version].
Fred Drakef9e1f651998-03-14 07:00:41 +0000104For example, \code{'BaseHTTP/0.2'}.
Fred Drakefc576191998-04-04 07:15:02 +0000105\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000106
Fred Drakefc576191998-04-04 07:15:02 +0000107\begin{memberdesc}{sys_version}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000108Contains the Python system version, in a form usable by the
Fred Drakef9e1f651998-03-14 07:00:41 +0000109\member{version_string} method and the \member{server_version} class
110variable. For example, \code{'Python/1.4'}.
Fred Drakefc576191998-04-04 07:15:02 +0000111\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000112
Fred Drakefc576191998-04-04 07:15:02 +0000113\begin{memberdesc}{error_message_format}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000114Specifies a format string for building an error response to the
115client. It uses parenthesized, keyed format specifiers, so the
116format operand must be a dictionary. The \var{code} key should
Thomas Woutersf8316632000-07-16 19:01:10 +0000117be an integer, specifying the numeric HTTP error code value.
Guido van Rossum9cb64801997-12-29 20:01:55 +0000118\var{message} should be a string containing a (detailed) error
119message of what occurred, and \var{explain} should be an
120explanation of the error code number. Default \var{message}
121and \var{explain} values can found in the \var{responses}
122class variable.
Fred Drakefc576191998-04-04 07:15:02 +0000123\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000124
Fred Drakefc576191998-04-04 07:15:02 +0000125\begin{memberdesc}{protocol_version}
Martin v. Löwis587c98c2002-03-17 18:37:22 +0000126This specifies the HTTP protocol version used in responses. If set
127to \code{'HTTP/1.1'}, the server will permit HTTP persistent
128connections; however, your server \emph{must} then include an
129accurate \code{Content-Length} header (using \method{send_header()})
130in all of its responses to clients. For backwards compatibility,
131the setting defaults to \code{'HTTP/1.0'}.
Fred Drakefc576191998-04-04 07:15:02 +0000132\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000133
Fred Drakefc576191998-04-04 07:15:02 +0000134\begin{memberdesc}{MessageClass}
Fred Drakef9e1f651998-03-14 07:00:41 +0000135Specifies a \class{rfc822.Message}-like class to parse HTTP
136headers. Typically, this is not overridden, and it defaults to
137\class{mimetools.Message}.
138\withsubitem{(in module mimetools)}{\ttindex{Message}}
Fred Drakefc576191998-04-04 07:15:02 +0000139\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000140
Fred Drakefc576191998-04-04 07:15:02 +0000141\begin{memberdesc}{responses}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000142This variable contains a mapping of error code integers to two-element
143tuples containing a short and long message. For example,
Fred Drakef9e1f651998-03-14 07:00:41 +0000144\code{\{\var{code}: (\var{shortmessage}, \var{longmessage})\}}. The
Guido van Rossum9cb64801997-12-29 20:01:55 +0000145\var{shortmessage} is usually used as the \var{message} key in an
146error response, and \var{longmessage} as the \var{explain} key
Fred Drakef9e1f651998-03-14 07:00:41 +0000147(see the \member{error_message_format} class variable).
Fred Drakefc576191998-04-04 07:15:02 +0000148\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000149
Guido van Rossum9cb64801997-12-29 20:01:55 +0000150
Fred Drakef9e1f651998-03-14 07:00:41 +0000151A \class{BaseHTTPRequestHandler} instance has the following methods:
Guido van Rossum9cb64801997-12-29 20:01:55 +0000152
Fred Drakefc576191998-04-04 07:15:02 +0000153\begin{methoddesc}{handle}{}
Martin v. Löwis587c98c2002-03-17 18:37:22 +0000154Calls \method{handle_one_request()} once (or, if persistent connections
155are enabled, multiple times) to handle incoming HTTP requests.
156You should never need to override it; instead, implement appropriate
157\method{do_*()} methods.
158\end{methoddesc}
159
160\begin{methoddesc}{handle_one_request}{}
161This method will parse and dispatch
162the request to the appropriate \method{do_*()} method. You should
163never need to override it.
Fred Drakefc576191998-04-04 07:15:02 +0000164\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000165
Fred Drakefc576191998-04-04 07:15:02 +0000166\begin{methoddesc}{send_error}{code\optional{, message}}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000167Sends and logs a complete error reply to the client. The numeric
168\var{code} specifies the HTTP error code, with \var{message} as
169optional, more specific text. A complete set of headers is sent,
Fred Drakef9e1f651998-03-14 07:00:41 +0000170followed by text composed using the \member{error_message_format}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000171class variable.
Fred Drakefc576191998-04-04 07:15:02 +0000172\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000173
Fred Drakefc576191998-04-04 07:15:02 +0000174\begin{methoddesc}{send_response}{code\optional{, message}}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000175Sends a response header and logs the accepted request. The HTTP
176response line is sent, followed by \emph{Server} and \emph{Date}
177headers. The values for these two headers are picked up from the
Fred Drakef9e1f651998-03-14 07:00:41 +0000178\method{version_string()} and \method{date_time_string()} methods,
Guido van Rossum9cb64801997-12-29 20:01:55 +0000179respectively.
Fred Drakefc576191998-04-04 07:15:02 +0000180\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000181
Fred Drakefc576191998-04-04 07:15:02 +0000182\begin{methoddesc}{send_header}{keyword, value}
Fred Drake6fbf7032004-04-29 02:47:38 +0000183Writes a specific HTTP header to the output stream. \var{keyword}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000184should specify the header keyword, with \var{value} specifying
185its value.
Fred Drakefc576191998-04-04 07:15:02 +0000186\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000187
Fred Drakefc576191998-04-04 07:15:02 +0000188\begin{methoddesc}{end_headers}{}
Fred Drake6fbf7032004-04-29 02:47:38 +0000189Sends a blank line, indicating the end of the HTTP headers in
Guido van Rossum9cb64801997-12-29 20:01:55 +0000190the response.
Fred Drakefc576191998-04-04 07:15:02 +0000191\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000192
Fred Drakefc576191998-04-04 07:15:02 +0000193\begin{methoddesc}{log_request}{\optional{code\optional{, size}}}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000194Logs an accepted (successful) request. \var{code} should specify
195the numeric HTTP code associated with the response. If a size of
196the response is available, then it should be passed as the
197\var{size} parameter.
Fred Drakefc576191998-04-04 07:15:02 +0000198\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000199
Fred Drakefc576191998-04-04 07:15:02 +0000200\begin{methoddesc}{log_error}{...}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000201Logs an error when a request cannot be fulfilled. By default,
Fred Drakef9e1f651998-03-14 07:00:41 +0000202it passes the message to \method{log_message()}, so it takes the
Guido van Rossum9cb64801997-12-29 20:01:55 +0000203same arguments (\var{format} and additional values).
Fred Drakefc576191998-04-04 07:15:02 +0000204\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000205
Fred Drakefc576191998-04-04 07:15:02 +0000206\begin{methoddesc}{log_message}{format, ...}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000207Logs an arbitrary message to \code{sys.stderr}. This is typically
208overridden to create custom error logging mechanisms. The
209\var{format} argument is a standard printf-style format string,
Fred Drakef9e1f651998-03-14 07:00:41 +0000210where the additional arguments to \method{log_message()} are applied
Guido van Rossum9cb64801997-12-29 20:01:55 +0000211as inputs to the formatting. The client address and current date
212and time are prefixed to every message logged.
Fred Drakefc576191998-04-04 07:15:02 +0000213\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000214
Fred Drakefc576191998-04-04 07:15:02 +0000215\begin{methoddesc}{version_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000216Returns the server software's version string. This is a combination
Fred Drakef9e1f651998-03-14 07:00:41 +0000217of the \member{server_version} and \member{sys_version} class variables.
Fred Drakefc576191998-04-04 07:15:02 +0000218\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000219
Fred Drakefc576191998-04-04 07:15:02 +0000220\begin{methoddesc}{date_time_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000221Returns the current date and time, formatted for a message header.
Fred Drakefc576191998-04-04 07:15:02 +0000222\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000223
Fred Drakefc576191998-04-04 07:15:02 +0000224\begin{methoddesc}{log_data_time_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000225Returns the current date and time, formatted for logging.
Fred Drakefc576191998-04-04 07:15:02 +0000226\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000227
Fred Drakefc576191998-04-04 07:15:02 +0000228\begin{methoddesc}{address_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000229Returns the client address, formatted for logging. A name lookup
230is performed on the client's IP address.
Fred Drakefc576191998-04-04 07:15:02 +0000231\end{methoddesc}
Fred Drakec46864e1999-06-14 19:49:50 +0000232
233
234\begin{seealso}
235 \seemodule{CGIHTTPServer}{Extended request handler that supports CGI
236 scripts.}
237
238 \seemodule{SimpleHTTPServer}{Basic request handler that limits response
239 to files actually under the document root.}
240\end{seealso}