blob: 0a8a0fc81e20466991201bff6f2715ec1d5a4a40 [file] [log] [blame]
Fred Drake295da241998-08-10 19:42:37 +00001\section{\module{BaseHTTPServer} ---
2 Basic HTTP server.}
Fred Drakeb91e9341998-07-23 17:59:49 +00003\declaremodule{standard}{BaseHTTPServer}
4
Fred Drake72d157e1998-08-06 21:23:17 +00005\modulesynopsis{Basic HTTP server (base class for
6\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 +000014
15This module defines two classes for implementing HTTP servers
16(web servers). Usually, this module isn't used directly, but is used
17as a basis for building functioning web servers. See the
Fred Drakef9e1f651998-03-14 07:00:41 +000018\module{SimpleHTTPServer} and \module{CGIHTTPServer} modules.
19\refstmodindex{SimpleHTTPServer}
20\refstmodindex{CGIHTTPServer}
Guido van Rossum9cb64801997-12-29 20:01:55 +000021
Fred Drakef9e1f651998-03-14 07:00:41 +000022The first class, \class{HTTPServer}, is a
23\class{SocketServer.TCPServer} subclass. It creates and listens at the
24web socket, dispatching the requests to a handler. Code to create and
25run the server looks like this:
Guido van Rossum9cb64801997-12-29 20:01:55 +000026
Fred Drake19479911998-02-13 06:58:54 +000027\begin{verbatim}
Guido van Rossum9cb64801997-12-29 20:01:55 +000028def run(server_class=BaseHTTPServer.HTTPServer,
29 handler_class=BaseHTTPServer.BaseHTTPRequestHandler):
Fred Drakefc576191998-04-04 07:15:02 +000030 server_address = ('', 8000)
31 httpd = server_class(server_address, handler_class)
32 httpd.serve_forever()
Fred Drake19479911998-02-13 06:58:54 +000033\end{verbatim}
Guido van Rossum9cb64801997-12-29 20:01:55 +000034
Fred Drakefc576191998-04-04 07:15:02 +000035\begin{classdesc}{HTTPServer}{server_address, RequestHandlerClass}
36This class builds on the \class{TCPServer} class by
Fred Drakef9e1f651998-03-14 07:00:41 +000037storing the server address as instance
38variables named \member{server_name} and \member{server_port}. The
39server is accessible by the handler, typically through the handler's
40\member{server} instance variable.
Fred Drakefc576191998-04-04 07:15:02 +000041\end{classdesc}
Fred Drakef9e1f651998-03-14 07:00:41 +000042
Fred Drakefc576191998-04-04 07:15:02 +000043\begin{classdesc}{BaseHTTPRequestHandler}{request, client_address, server}
44This class is used
Guido van Rossum9cb64801997-12-29 20:01:55 +000045to handle the HTTP requests that arrive at the server. By itself,
46it cannot respond to any actual HTTP requests; it must be subclassed
47to handle each request method (e.g. GET or POST).
Fred Drakef9e1f651998-03-14 07:00:41 +000048\class{BaseHTTPRequestHandler} provides a number of class and instance
Guido van Rossum9cb64801997-12-29 20:01:55 +000049variables, and methods for use by subclasses.
50
51The handler will parse the request and the headers, then call a
52method specific to the request type. The method name is constructed
Fred Drakefc576191998-04-04 07:15:02 +000053from the request. For example, for the request method \samp{SPAM}, the
Fred Drakef9e1f651998-03-14 07:00:41 +000054\method{do_SPAM()} method will be called with no arguments. All of
Fred Drakefc576191998-04-04 07:15:02 +000055the relevant information is stored in instance variables of the
56handler. Subclasses should not need to override or extend the
57\method{__init__()} method.
58\end{classdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000059
Guido van Rossum9cb64801997-12-29 20:01:55 +000060
Fred Drakef9e1f651998-03-14 07:00:41 +000061\class{BaseHTTPRequestHandler} has the following instance variables:
Guido van Rossum9cb64801997-12-29 20:01:55 +000062
Fred Drakefc576191998-04-04 07:15:02 +000063\begin{memberdesc}{client_address}
Fred Drakef9e1f651998-03-14 07:00:41 +000064Contains a tuple of the form \code{(\var{host}, \var{port})} referring
65to the client's address.
Fred Drakefc576191998-04-04 07:15:02 +000066\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000067
Fred Drakefc576191998-04-04 07:15:02 +000068\begin{memberdesc}{command}
Fred Drakef9e1f651998-03-14 07:00:41 +000069Contains the command (request type). For example, \code{'GET'}.
Fred Drakefc576191998-04-04 07:15:02 +000070\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000071
Fred Drakefc576191998-04-04 07:15:02 +000072\begin{memberdesc}{path}
Guido van Rossum9cb64801997-12-29 20:01:55 +000073Contains the request path.
Fred Drakefc576191998-04-04 07:15:02 +000074\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000075
Fred Drakefc576191998-04-04 07:15:02 +000076\begin{memberdesc}{request_version}
Guido van Rossum9cb64801997-12-29 20:01:55 +000077Contains the version string from the request. For example,
Fred Drakef9e1f651998-03-14 07:00:41 +000078\code{'HTTP/1.0'}.
Fred Drakefc576191998-04-04 07:15:02 +000079\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000080
Fred Drakefc576191998-04-04 07:15:02 +000081\begin{memberdesc}{headers}
Fred Drakef9e1f651998-03-14 07:00:41 +000082Holds an instance of the class specified by the \member{MessageClass}
Guido van Rossum9cb64801997-12-29 20:01:55 +000083class variable. This instance parses and manages the headers in
84the HTTP request.
Fred Drakefc576191998-04-04 07:15:02 +000085\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000086
Fred Drakefc576191998-04-04 07:15:02 +000087\begin{memberdesc}{rfile}
Guido van Rossum9cb64801997-12-29 20:01:55 +000088Contains an input stream, positioned at the start of the optional
89input data.
Fred Drakefc576191998-04-04 07:15:02 +000090\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000091
Fred Drakefc576191998-04-04 07:15:02 +000092\begin{memberdesc}{wfile}
Guido van Rossum9cb64801997-12-29 20:01:55 +000093Contains the output stream for writing a response back to the client.
94Proper adherance to the HTTP protocol must be used when writing
95to this stream.
Fred Drakefc576191998-04-04 07:15:02 +000096\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000097
Guido van Rossum9cb64801997-12-29 20:01:55 +000098
Fred Drakefc576191998-04-04 07:15:02 +000099\class{BaseHTTPRequestHandler} has the following class variables:
Guido van Rossum9cb64801997-12-29 20:01:55 +0000100
Fred Drakefc576191998-04-04 07:15:02 +0000101\begin{memberdesc}{server_version}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000102Specifies the server software version. You may want to override
103this.
104The format is multiple whitespace-separated strings,
105where each string is of the form name[/version].
Fred Drakef9e1f651998-03-14 07:00:41 +0000106For example, \code{'BaseHTTP/0.2'}.
Fred Drakefc576191998-04-04 07:15:02 +0000107\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000108
Fred Drakefc576191998-04-04 07:15:02 +0000109\begin{memberdesc}{sys_version}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000110Contains the Python system version, in a form usable by the
Fred Drakef9e1f651998-03-14 07:00:41 +0000111\member{version_string} method and the \member{server_version} class
112variable. For example, \code{'Python/1.4'}.
Fred Drakefc576191998-04-04 07:15:02 +0000113\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000114
Fred Drakefc576191998-04-04 07:15:02 +0000115\begin{memberdesc}{error_message_format}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000116Specifies a format string for building an error response to the
117client. It uses parenthesized, keyed format specifiers, so the
118format operand must be a dictionary. The \var{code} key should
119be an integer, specifing the numeric HTTP error code value.
120\var{message} should be a string containing a (detailed) error
121message of what occurred, and \var{explain} should be an
122explanation of the error code number. Default \var{message}
123and \var{explain} values can found in the \var{responses}
124class variable.
Fred Drakefc576191998-04-04 07:15:02 +0000125\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000126
Fred Drakefc576191998-04-04 07:15:02 +0000127\begin{memberdesc}{protocol_version}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000128This specifies the HTTP protocol version used in responses.
129Typically, this should not be overridden. Defaults to
Fred Drakef9e1f651998-03-14 07:00:41 +0000130\code{'HTTP/1.0'}.
Fred Drakefc576191998-04-04 07:15:02 +0000131\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000132
Fred Drakefc576191998-04-04 07:15:02 +0000133\begin{memberdesc}{MessageClass}
Fred Drakef9e1f651998-03-14 07:00:41 +0000134Specifies a \class{rfc822.Message}-like class to parse HTTP
135headers. Typically, this is not overridden, and it defaults to
136\class{mimetools.Message}.
137\withsubitem{(in module mimetools)}{\ttindex{Message}}
Fred Drakefc576191998-04-04 07:15:02 +0000138\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000139
Fred Drakefc576191998-04-04 07:15:02 +0000140\begin{memberdesc}{responses}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000141This variable contains a mapping of error code integers to two-element
142tuples containing a short and long message. For example,
Fred Drakef9e1f651998-03-14 07:00:41 +0000143\code{\{\var{code}: (\var{shortmessage}, \var{longmessage})\}}. The
Guido van Rossum9cb64801997-12-29 20:01:55 +0000144\var{shortmessage} is usually used as the \var{message} key in an
145error response, and \var{longmessage} as the \var{explain} key
Fred Drakef9e1f651998-03-14 07:00:41 +0000146(see the \member{error_message_format} class variable).
Fred Drakefc576191998-04-04 07:15:02 +0000147\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000148
Guido van Rossum9cb64801997-12-29 20:01:55 +0000149
Fred Drakef9e1f651998-03-14 07:00:41 +0000150A \class{BaseHTTPRequestHandler} instance has the following methods:
Guido van Rossum9cb64801997-12-29 20:01:55 +0000151
Fred Drakefc576191998-04-04 07:15:02 +0000152\begin{methoddesc}{handle}{}
Fred Drakef9e1f651998-03-14 07:00:41 +0000153Overrides the superclass' \method{handle()} method to provide the
Guido van Rossum9cb64801997-12-29 20:01:55 +0000154specific handler behavior. This method will parse and dispatch
Fred Drakefc576191998-04-04 07:15:02 +0000155the request to the appropriate \method{do_*()} method.
156\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000157
Fred Drakefc576191998-04-04 07:15:02 +0000158\begin{methoddesc}{send_error}{code\optional{, message}}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000159Sends and logs a complete error reply to the client. The numeric
160\var{code} specifies the HTTP error code, with \var{message} as
161optional, more specific text. A complete set of headers is sent,
Fred Drakef9e1f651998-03-14 07:00:41 +0000162followed by text composed using the \member{error_message_format}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000163class variable.
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_response}{code\optional{, message}}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000167Sends a response header and logs the accepted request. The HTTP
168response line is sent, followed by \emph{Server} and \emph{Date}
169headers. The values for these two headers are picked up from the
Fred Drakef9e1f651998-03-14 07:00:41 +0000170\method{version_string()} and \method{date_time_string()} methods,
Guido van Rossum9cb64801997-12-29 20:01:55 +0000171respectively.
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_header}{keyword, value}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000175Writes a specific MIME header to the output stream. \var{keyword}
176should specify the header keyword, with \var{value} specifying
177its value.
Fred Drakefc576191998-04-04 07:15:02 +0000178\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000179
Fred Drakefc576191998-04-04 07:15:02 +0000180\begin{methoddesc}{end_headers}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000181Sends a blank line, indicating the end of the MIME headers in
182the response.
Fred Drakefc576191998-04-04 07:15:02 +0000183\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000184
Fred Drakefc576191998-04-04 07:15:02 +0000185\begin{methoddesc}{log_request}{\optional{code\optional{, size}}}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000186Logs an accepted (successful) request. \var{code} should specify
187the numeric HTTP code associated with the response. If a size of
188the response is available, then it should be passed as the
189\var{size} parameter.
Fred Drakefc576191998-04-04 07:15:02 +0000190\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000191
Fred Drakefc576191998-04-04 07:15:02 +0000192\begin{methoddesc}{log_error}{...}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000193Logs an error when a request cannot be fulfilled. By default,
Fred Drakef9e1f651998-03-14 07:00:41 +0000194it passes the message to \method{log_message()}, so it takes the
Guido van Rossum9cb64801997-12-29 20:01:55 +0000195same arguments (\var{format} and additional values).
Fred Drakefc576191998-04-04 07:15:02 +0000196\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000197
Fred Drakefc576191998-04-04 07:15:02 +0000198\begin{methoddesc}{log_message}{format, ...}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000199Logs an arbitrary message to \code{sys.stderr}. This is typically
200overridden to create custom error logging mechanisms. The
201\var{format} argument is a standard printf-style format string,
Fred Drakef9e1f651998-03-14 07:00:41 +0000202where the additional arguments to \method{log_message()} are applied
Guido van Rossum9cb64801997-12-29 20:01:55 +0000203as inputs to the formatting. The client address and current date
204and time are prefixed to every message logged.
Fred Drakefc576191998-04-04 07:15:02 +0000205\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000206
Fred Drakefc576191998-04-04 07:15:02 +0000207\begin{methoddesc}{version_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000208Returns the server software's version string. This is a combination
Fred Drakef9e1f651998-03-14 07:00:41 +0000209of the \member{server_version} and \member{sys_version} class variables.
Fred Drakefc576191998-04-04 07:15:02 +0000210\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000211
Fred Drakefc576191998-04-04 07:15:02 +0000212\begin{methoddesc}{date_time_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000213Returns the current date and time, formatted for a message header.
Fred Drakefc576191998-04-04 07:15:02 +0000214\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000215
Fred Drakefc576191998-04-04 07:15:02 +0000216\begin{methoddesc}{log_data_time_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000217Returns the current date and time, formatted for logging.
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}{address_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000221Returns the client address, formatted for logging. A name lookup
222is performed on the client's IP address.
Fred Drakefc576191998-04-04 07:15:02 +0000223\end{methoddesc}