blob: e680305261a0e3f91b515dc3d1603518158b9420 [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
Fred Drake72d157e1998-08-06 21:23:17 +00004\modulesynopsis{Basic HTTP server (base class for
5\class{SimpleHTTPServer} and \class{CGIHTTPServer}).}
Fred Drakeb91e9341998-07-23 17:59:49 +00006
Guido van Rossum9cb64801997-12-29 20:01:55 +00007
8\indexii{WWW}{server}
9\indexii{HTTP}{protocol}
10\index{URL}
11\index{httpd}
12
Guido van Rossum9cb64801997-12-29 20:01:55 +000013
14This module defines two classes for implementing HTTP servers
15(web servers). Usually, this module isn't used directly, but is used
16as a basis for building functioning web servers. See the
Fred Drakef9e1f651998-03-14 07:00:41 +000017\module{SimpleHTTPServer} and \module{CGIHTTPServer} modules.
18\refstmodindex{SimpleHTTPServer}
19\refstmodindex{CGIHTTPServer}
Guido van Rossum9cb64801997-12-29 20:01:55 +000020
Fred Drakef9e1f651998-03-14 07:00:41 +000021The first class, \class{HTTPServer}, is a
22\class{SocketServer.TCPServer} subclass. It creates and listens at the
23web socket, dispatching the requests to a handler. Code to create and
24run the server looks like this:
Guido van Rossum9cb64801997-12-29 20:01:55 +000025
Fred Drake19479911998-02-13 06:58:54 +000026\begin{verbatim}
Guido van Rossum9cb64801997-12-29 20:01:55 +000027def run(server_class=BaseHTTPServer.HTTPServer,
28 handler_class=BaseHTTPServer.BaseHTTPRequestHandler):
Fred Drakefc576191998-04-04 07:15:02 +000029 server_address = ('', 8000)
30 httpd = server_class(server_address, handler_class)
31 httpd.serve_forever()
Fred Drake19479911998-02-13 06:58:54 +000032\end{verbatim}
Guido van Rossum9cb64801997-12-29 20:01:55 +000033
Fred Drakefc576191998-04-04 07:15:02 +000034\begin{classdesc}{HTTPServer}{server_address, RequestHandlerClass}
35This class builds on the \class{TCPServer} class by
Fred Drakef9e1f651998-03-14 07:00:41 +000036storing the server address as instance
37variables named \member{server_name} and \member{server_port}. The
38server is accessible by the handler, typically through the handler's
39\member{server} instance variable.
Fred Drakefc576191998-04-04 07:15:02 +000040\end{classdesc}
Fred Drakef9e1f651998-03-14 07:00:41 +000041
Fred Drakefc576191998-04-04 07:15:02 +000042\begin{classdesc}{BaseHTTPRequestHandler}{request, client_address, server}
43This class is used
Guido van Rossum9cb64801997-12-29 20:01:55 +000044to handle the HTTP requests that arrive at the server. By itself,
45it cannot respond to any actual HTTP requests; it must be subclassed
46to handle each request method (e.g. GET or POST).
Fred Drakef9e1f651998-03-14 07:00:41 +000047\class{BaseHTTPRequestHandler} provides a number of class and instance
Guido van Rossum9cb64801997-12-29 20:01:55 +000048variables, and methods for use by subclasses.
49
50The handler will parse the request and the headers, then call a
51method specific to the request type. The method name is constructed
Fred Drakefc576191998-04-04 07:15:02 +000052from the request. For example, for the request method \samp{SPAM}, the
Fred Drakef9e1f651998-03-14 07:00:41 +000053\method{do_SPAM()} method will be called with no arguments. All of
Fred Drakefc576191998-04-04 07:15:02 +000054the relevant information is stored in instance variables of the
55handler. Subclasses should not need to override or extend the
56\method{__init__()} method.
57\end{classdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000058
Guido van Rossum9cb64801997-12-29 20:01:55 +000059
Fred Drakef9e1f651998-03-14 07:00:41 +000060\class{BaseHTTPRequestHandler} has the following instance variables:
Guido van Rossum9cb64801997-12-29 20:01:55 +000061
Fred Drakefc576191998-04-04 07:15:02 +000062\begin{memberdesc}{client_address}
Fred Drakef9e1f651998-03-14 07:00:41 +000063Contains a tuple of the form \code{(\var{host}, \var{port})} referring
64to the client's address.
Fred Drakefc576191998-04-04 07:15:02 +000065\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000066
Fred Drakefc576191998-04-04 07:15:02 +000067\begin{memberdesc}{command}
Fred Drakef9e1f651998-03-14 07:00:41 +000068Contains the command (request type). For example, \code{'GET'}.
Fred Drakefc576191998-04-04 07:15:02 +000069\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000070
Fred Drakefc576191998-04-04 07:15:02 +000071\begin{memberdesc}{path}
Guido van Rossum9cb64801997-12-29 20:01:55 +000072Contains the request path.
Fred Drakefc576191998-04-04 07:15:02 +000073\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000074
Fred Drakefc576191998-04-04 07:15:02 +000075\begin{memberdesc}{request_version}
Guido van Rossum9cb64801997-12-29 20:01:55 +000076Contains the version string from the request. For example,
Fred Drakef9e1f651998-03-14 07:00:41 +000077\code{'HTTP/1.0'}.
Fred Drakefc576191998-04-04 07:15:02 +000078\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000079
Fred Drakefc576191998-04-04 07:15:02 +000080\begin{memberdesc}{headers}
Fred Drakef9e1f651998-03-14 07:00:41 +000081Holds an instance of the class specified by the \member{MessageClass}
Guido van Rossum9cb64801997-12-29 20:01:55 +000082class variable. This instance parses and manages the headers in
83the HTTP request.
Fred Drakefc576191998-04-04 07:15:02 +000084\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000085
Fred Drakefc576191998-04-04 07:15:02 +000086\begin{memberdesc}{rfile}
Guido van Rossum9cb64801997-12-29 20:01:55 +000087Contains an input stream, positioned at the start of the optional
88input data.
Fred Drakefc576191998-04-04 07:15:02 +000089\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000090
Fred Drakefc576191998-04-04 07:15:02 +000091\begin{memberdesc}{wfile}
Guido van Rossum9cb64801997-12-29 20:01:55 +000092Contains the output stream for writing a response back to the client.
93Proper adherance to the HTTP protocol must be used when writing
94to this stream.
Fred Drakefc576191998-04-04 07:15:02 +000095\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000096
Guido van Rossum9cb64801997-12-29 20:01:55 +000097
Fred Drakefc576191998-04-04 07:15:02 +000098\class{BaseHTTPRequestHandler} has the following class variables:
Guido van Rossum9cb64801997-12-29 20:01:55 +000099
Fred Drakefc576191998-04-04 07:15:02 +0000100\begin{memberdesc}{server_version}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000101Specifies the server software version. You may want to override
102this.
103The format is multiple whitespace-separated strings,
104where each string is of the form name[/version].
Fred Drakef9e1f651998-03-14 07:00:41 +0000105For example, \code{'BaseHTTP/0.2'}.
Fred Drakefc576191998-04-04 07:15:02 +0000106\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000107
Fred Drakefc576191998-04-04 07:15:02 +0000108\begin{memberdesc}{sys_version}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000109Contains the Python system version, in a form usable by the
Fred Drakef9e1f651998-03-14 07:00:41 +0000110\member{version_string} method and the \member{server_version} class
111variable. For example, \code{'Python/1.4'}.
Fred Drakefc576191998-04-04 07:15:02 +0000112\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000113
Fred Drakefc576191998-04-04 07:15:02 +0000114\begin{memberdesc}{error_message_format}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000115Specifies a format string for building an error response to the
116client. It uses parenthesized, keyed format specifiers, so the
117format operand must be a dictionary. The \var{code} key should
118be an integer, specifing the numeric HTTP error code value.
119\var{message} should be a string containing a (detailed) error
120message of what occurred, and \var{explain} should be an
121explanation of the error code number. Default \var{message}
122and \var{explain} values can found in the \var{responses}
123class variable.
Fred Drakefc576191998-04-04 07:15:02 +0000124\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000125
Fred Drakefc576191998-04-04 07:15:02 +0000126\begin{memberdesc}{protocol_version}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000127This specifies the HTTP protocol version used in responses.
128Typically, this should not be overridden. Defaults to
Fred Drakef9e1f651998-03-14 07:00:41 +0000129\code{'HTTP/1.0'}.
Fred Drakefc576191998-04-04 07:15:02 +0000130\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000131
Fred Drakefc576191998-04-04 07:15:02 +0000132\begin{memberdesc}{MessageClass}
Fred Drakef9e1f651998-03-14 07:00:41 +0000133Specifies a \class{rfc822.Message}-like class to parse HTTP
134headers. Typically, this is not overridden, and it defaults to
135\class{mimetools.Message}.
136\withsubitem{(in module mimetools)}{\ttindex{Message}}
Fred Drakefc576191998-04-04 07:15:02 +0000137\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000138
Fred Drakefc576191998-04-04 07:15:02 +0000139\begin{memberdesc}{responses}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000140This variable contains a mapping of error code integers to two-element
141tuples containing a short and long message. For example,
Fred Drakef9e1f651998-03-14 07:00:41 +0000142\code{\{\var{code}: (\var{shortmessage}, \var{longmessage})\}}. The
Guido van Rossum9cb64801997-12-29 20:01:55 +0000143\var{shortmessage} is usually used as the \var{message} key in an
144error response, and \var{longmessage} as the \var{explain} key
Fred Drakef9e1f651998-03-14 07:00:41 +0000145(see the \member{error_message_format} class variable).
Fred Drakefc576191998-04-04 07:15:02 +0000146\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000147
Guido van Rossum9cb64801997-12-29 20:01:55 +0000148
Fred Drakef9e1f651998-03-14 07:00:41 +0000149A \class{BaseHTTPRequestHandler} instance has the following methods:
Guido van Rossum9cb64801997-12-29 20:01:55 +0000150
Fred Drakefc576191998-04-04 07:15:02 +0000151\begin{methoddesc}{handle}{}
Fred Drakef9e1f651998-03-14 07:00:41 +0000152Overrides the superclass' \method{handle()} method to provide the
Guido van Rossum9cb64801997-12-29 20:01:55 +0000153specific handler behavior. This method will parse and dispatch
Fred Drakefc576191998-04-04 07:15:02 +0000154the request to the appropriate \method{do_*()} method.
155\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000156
Fred Drakefc576191998-04-04 07:15:02 +0000157\begin{methoddesc}{send_error}{code\optional{, message}}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000158Sends and logs a complete error reply to the client. The numeric
159\var{code} specifies the HTTP error code, with \var{message} as
160optional, more specific text. A complete set of headers is sent,
Fred Drakef9e1f651998-03-14 07:00:41 +0000161followed by text composed using the \member{error_message_format}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000162class variable.
Fred Drakefc576191998-04-04 07:15:02 +0000163\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000164
Fred Drakefc576191998-04-04 07:15:02 +0000165\begin{methoddesc}{send_response}{code\optional{, message}}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000166Sends a response header and logs the accepted request. The HTTP
167response line is sent, followed by \emph{Server} and \emph{Date}
168headers. The values for these two headers are picked up from the
Fred Drakef9e1f651998-03-14 07:00:41 +0000169\method{version_string()} and \method{date_time_string()} methods,
Guido van Rossum9cb64801997-12-29 20:01:55 +0000170respectively.
Fred Drakefc576191998-04-04 07:15:02 +0000171\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000172
Fred Drakefc576191998-04-04 07:15:02 +0000173\begin{methoddesc}{send_header}{keyword, value}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000174Writes a specific MIME header to the output stream. \var{keyword}
175should specify the header keyword, with \var{value} specifying
176its value.
Fred Drakefc576191998-04-04 07:15:02 +0000177\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000178
Fred Drakefc576191998-04-04 07:15:02 +0000179\begin{methoddesc}{end_headers}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000180Sends a blank line, indicating the end of the MIME headers in
181the response.
Fred Drakefc576191998-04-04 07:15:02 +0000182\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000183
Fred Drakefc576191998-04-04 07:15:02 +0000184\begin{methoddesc}{log_request}{\optional{code\optional{, size}}}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000185Logs an accepted (successful) request. \var{code} should specify
186the numeric HTTP code associated with the response. If a size of
187the response is available, then it should be passed as the
188\var{size} parameter.
Fred Drakefc576191998-04-04 07:15:02 +0000189\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000190
Fred Drakefc576191998-04-04 07:15:02 +0000191\begin{methoddesc}{log_error}{...}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000192Logs an error when a request cannot be fulfilled. By default,
Fred Drakef9e1f651998-03-14 07:00:41 +0000193it passes the message to \method{log_message()}, so it takes the
Guido van Rossum9cb64801997-12-29 20:01:55 +0000194same arguments (\var{format} and additional values).
Fred Drakefc576191998-04-04 07:15:02 +0000195\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000196
Fred Drakefc576191998-04-04 07:15:02 +0000197\begin{methoddesc}{log_message}{format, ...}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000198Logs an arbitrary message to \code{sys.stderr}. This is typically
199overridden to create custom error logging mechanisms. The
200\var{format} argument is a standard printf-style format string,
Fred Drakef9e1f651998-03-14 07:00:41 +0000201where the additional arguments to \method{log_message()} are applied
Guido van Rossum9cb64801997-12-29 20:01:55 +0000202as inputs to the formatting. The client address and current date
203and time are prefixed to every message logged.
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}{version_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000207Returns the server software's version string. This is a combination
Fred Drakef9e1f651998-03-14 07:00:41 +0000208of the \member{server_version} and \member{sys_version} class variables.
Fred Drakefc576191998-04-04 07:15:02 +0000209\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000210
Fred Drakefc576191998-04-04 07:15:02 +0000211\begin{methoddesc}{date_time_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000212Returns the current date and time, formatted for a message header.
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}{log_data_time_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000216Returns the current date and time, formatted for logging.
Fred Drakefc576191998-04-04 07:15:02 +0000217\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000218
Fred Drakefc576191998-04-04 07:15:02 +0000219\begin{methoddesc}{address_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000220Returns the client address, formatted for logging. A name lookup
221is performed on the client's IP address.
Fred Drakefc576191998-04-04 07:15:02 +0000222\end{methoddesc}