blob: df04202040e6d08a24f0710514cc1a26f6918dad [file] [log] [blame]
Fred Drakefc576191998-04-04 07:15:02 +00001\section{Standard Module \module{BaseHTTPServer}}
Fred Drakeb91e9341998-07-23 17:59:49 +00002\declaremodule{standard}{BaseHTTPServer}
3
4\modulesynopsis{Basic HTTP server (base class for SimpleHTTPServer and CGIHTTPServer).}
5
Guido van Rossum9cb64801997-12-29 20:01:55 +00006
7\indexii{WWW}{server}
8\indexii{HTTP}{protocol}
9\index{URL}
10\index{httpd}
11
Guido van Rossum9cb64801997-12-29 20:01:55 +000012
13This module defines two classes for implementing HTTP servers
14(web servers). Usually, this module isn't used directly, but is used
15as a basis for building functioning web servers. See the
Fred Drakef9e1f651998-03-14 07:00:41 +000016\module{SimpleHTTPServer} and \module{CGIHTTPServer} modules.
17\refstmodindex{SimpleHTTPServer}
18\refstmodindex{CGIHTTPServer}
Guido van Rossum9cb64801997-12-29 20:01:55 +000019
Fred Drakef9e1f651998-03-14 07:00:41 +000020The first class, \class{HTTPServer}, is a
21\class{SocketServer.TCPServer} subclass. It creates and listens at the
22web socket, dispatching the requests to a handler. Code to create and
23run 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.
92Proper adherance to the HTTP protocol must be used when writing
93to 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
117be an integer, specifing the numeric HTTP error code value.
118\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}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000126This specifies the HTTP protocol version used in responses.
127Typically, this should not be overridden. Defaults to
Fred Drakef9e1f651998-03-14 07:00:41 +0000128\code{'HTTP/1.0'}.
Fred Drakefc576191998-04-04 07:15:02 +0000129\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000130
Fred Drakefc576191998-04-04 07:15:02 +0000131\begin{memberdesc}{MessageClass}
Fred Drakef9e1f651998-03-14 07:00:41 +0000132Specifies a \class{rfc822.Message}-like class to parse HTTP
133headers. Typically, this is not overridden, and it defaults to
134\class{mimetools.Message}.
135\withsubitem{(in module mimetools)}{\ttindex{Message}}
Fred Drakefc576191998-04-04 07:15:02 +0000136\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000137
Fred Drakefc576191998-04-04 07:15:02 +0000138\begin{memberdesc}{responses}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000139This variable contains a mapping of error code integers to two-element
140tuples containing a short and long message. For example,
Fred Drakef9e1f651998-03-14 07:00:41 +0000141\code{\{\var{code}: (\var{shortmessage}, \var{longmessage})\}}. The
Guido van Rossum9cb64801997-12-29 20:01:55 +0000142\var{shortmessage} is usually used as the \var{message} key in an
143error response, and \var{longmessage} as the \var{explain} key
Fred Drakef9e1f651998-03-14 07:00:41 +0000144(see the \member{error_message_format} class variable).
Fred Drakefc576191998-04-04 07:15:02 +0000145\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000146
Guido van Rossum9cb64801997-12-29 20:01:55 +0000147
Fred Drakef9e1f651998-03-14 07:00:41 +0000148A \class{BaseHTTPRequestHandler} instance has the following methods:
Guido van Rossum9cb64801997-12-29 20:01:55 +0000149
Fred Drakefc576191998-04-04 07:15:02 +0000150\begin{methoddesc}{handle}{}
Fred Drakef9e1f651998-03-14 07:00:41 +0000151Overrides the superclass' \method{handle()} method to provide the
Guido van Rossum9cb64801997-12-29 20:01:55 +0000152specific handler behavior. This method will parse and dispatch
Fred Drakefc576191998-04-04 07:15:02 +0000153the request to the appropriate \method{do_*()} method.
154\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000155
Fred Drakefc576191998-04-04 07:15:02 +0000156\begin{methoddesc}{send_error}{code\optional{, message}}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000157Sends and logs a complete error reply to the client. The numeric
158\var{code} specifies the HTTP error code, with \var{message} as
159optional, more specific text. A complete set of headers is sent,
Fred Drakef9e1f651998-03-14 07:00:41 +0000160followed by text composed using the \member{error_message_format}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000161class variable.
Fred Drakefc576191998-04-04 07:15:02 +0000162\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000163
Fred Drakefc576191998-04-04 07:15:02 +0000164\begin{methoddesc}{send_response}{code\optional{, message}}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000165Sends a response header and logs the accepted request. The HTTP
166response line is sent, followed by \emph{Server} and \emph{Date}
167headers. The values for these two headers are picked up from the
Fred Drakef9e1f651998-03-14 07:00:41 +0000168\method{version_string()} and \method{date_time_string()} methods,
Guido van Rossum9cb64801997-12-29 20:01:55 +0000169respectively.
Fred Drakefc576191998-04-04 07:15:02 +0000170\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000171
Fred Drakefc576191998-04-04 07:15:02 +0000172\begin{methoddesc}{send_header}{keyword, value}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000173Writes a specific MIME header to the output stream. \var{keyword}
174should specify the header keyword, with \var{value} specifying
175its value.
Fred Drakefc576191998-04-04 07:15:02 +0000176\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000177
Fred Drakefc576191998-04-04 07:15:02 +0000178\begin{methoddesc}{end_headers}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000179Sends a blank line, indicating the end of the MIME headers in
180the response.
Fred Drakefc576191998-04-04 07:15:02 +0000181\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000182
Fred Drakefc576191998-04-04 07:15:02 +0000183\begin{methoddesc}{log_request}{\optional{code\optional{, size}}}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000184Logs an accepted (successful) request. \var{code} should specify
185the numeric HTTP code associated with the response. If a size of
186the response is available, then it should be passed as the
187\var{size} parameter.
Fred Drakefc576191998-04-04 07:15:02 +0000188\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000189
Fred Drakefc576191998-04-04 07:15:02 +0000190\begin{methoddesc}{log_error}{...}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000191Logs an error when a request cannot be fulfilled. By default,
Fred Drakef9e1f651998-03-14 07:00:41 +0000192it passes the message to \method{log_message()}, so it takes the
Guido van Rossum9cb64801997-12-29 20:01:55 +0000193same arguments (\var{format} and additional values).
Fred Drakefc576191998-04-04 07:15:02 +0000194\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000195
Fred Drakefc576191998-04-04 07:15:02 +0000196\begin{methoddesc}{log_message}{format, ...}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000197Logs an arbitrary message to \code{sys.stderr}. This is typically
198overridden to create custom error logging mechanisms. The
199\var{format} argument is a standard printf-style format string,
Fred Drakef9e1f651998-03-14 07:00:41 +0000200where the additional arguments to \method{log_message()} are applied
Guido van Rossum9cb64801997-12-29 20:01:55 +0000201as inputs to the formatting. The client address and current date
202and time are prefixed to every message logged.
Fred Drakefc576191998-04-04 07:15:02 +0000203\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000204
Fred Drakefc576191998-04-04 07:15:02 +0000205\begin{methoddesc}{version_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000206Returns the server software's version string. This is a combination
Fred Drakef9e1f651998-03-14 07:00:41 +0000207of the \member{server_version} and \member{sys_version} class variables.
Fred Drakefc576191998-04-04 07:15:02 +0000208\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000209
Fred Drakefc576191998-04-04 07:15:02 +0000210\begin{methoddesc}{date_time_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000211Returns the current date and time, formatted for a message header.
Fred Drakefc576191998-04-04 07:15:02 +0000212\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000213
Fred Drakefc576191998-04-04 07:15:02 +0000214\begin{methoddesc}{log_data_time_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000215Returns the current date and time, formatted for logging.
Fred Drakefc576191998-04-04 07:15:02 +0000216\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000217
Fred Drakefc576191998-04-04 07:15:02 +0000218\begin{methoddesc}{address_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000219Returns the client address, formatted for logging. A name lookup
220is performed on the client's IP address.
Fred Drakefc576191998-04-04 07:15:02 +0000221\end{methoddesc}