blob: 7ee55df0b13fb6de8d1f4da12f9ee64706f7a6de [file] [log] [blame]
Fred Drakefc576191998-04-04 07:15:02 +00001\section{Standard Module \module{BaseHTTPServer}}
Guido van Rossum9cb64801997-12-29 20:01:55 +00002\label{module-BaseHTTPServer}
3\stmodindex{BaseHTTPServer}
4
5\indexii{WWW}{server}
6\indexii{HTTP}{protocol}
7\index{URL}
8\index{httpd}
9
Guido van Rossum9cb64801997-12-29 20:01:55 +000010
11This module defines two classes for implementing HTTP servers
12(web servers). Usually, this module isn't used directly, but is used
13as a basis for building functioning web servers. See the
Fred Drakef9e1f651998-03-14 07:00:41 +000014\module{SimpleHTTPServer} and \module{CGIHTTPServer} modules.
15\refstmodindex{SimpleHTTPServer}
16\refstmodindex{CGIHTTPServer}
Guido van Rossum9cb64801997-12-29 20:01:55 +000017
Fred Drakef9e1f651998-03-14 07:00:41 +000018The first class, \class{HTTPServer}, is a
19\class{SocketServer.TCPServer} subclass. It creates and listens at the
20web socket, dispatching the requests to a handler. Code to create and
21run the server looks like this:
Guido van Rossum9cb64801997-12-29 20:01:55 +000022
Fred Drake19479911998-02-13 06:58:54 +000023\begin{verbatim}
Guido van Rossum9cb64801997-12-29 20:01:55 +000024def run(server_class=BaseHTTPServer.HTTPServer,
25 handler_class=BaseHTTPServer.BaseHTTPRequestHandler):
Fred Drakefc576191998-04-04 07:15:02 +000026 server_address = ('', 8000)
27 httpd = server_class(server_address, handler_class)
28 httpd.serve_forever()
Fred Drake19479911998-02-13 06:58:54 +000029\end{verbatim}
Guido van Rossum9cb64801997-12-29 20:01:55 +000030
Fred Drakefc576191998-04-04 07:15:02 +000031\begin{classdesc}{HTTPServer}{server_address, RequestHandlerClass}
32This class builds on the \class{TCPServer} class by
Fred Drakef9e1f651998-03-14 07:00:41 +000033storing the server address as instance
34variables named \member{server_name} and \member{server_port}. The
35server is accessible by the handler, typically through the handler's
36\member{server} instance variable.
Fred Drakefc576191998-04-04 07:15:02 +000037\end{classdesc}
Fred Drakef9e1f651998-03-14 07:00:41 +000038
Fred Drakefc576191998-04-04 07:15:02 +000039\begin{classdesc}{BaseHTTPRequestHandler}{request, client_address, server}
40This class is used
Guido van Rossum9cb64801997-12-29 20:01:55 +000041to handle the HTTP requests that arrive at the server. By itself,
42it cannot respond to any actual HTTP requests; it must be subclassed
43to handle each request method (e.g. GET or POST).
Fred Drakef9e1f651998-03-14 07:00:41 +000044\class{BaseHTTPRequestHandler} provides a number of class and instance
Guido van Rossum9cb64801997-12-29 20:01:55 +000045variables, and methods for use by subclasses.
46
47The handler will parse the request and the headers, then call a
48method specific to the request type. The method name is constructed
Fred Drakefc576191998-04-04 07:15:02 +000049from the request. For example, for the request method \samp{SPAM}, the
Fred Drakef9e1f651998-03-14 07:00:41 +000050\method{do_SPAM()} method will be called with no arguments. All of
Fred Drakefc576191998-04-04 07:15:02 +000051the relevant information is stored in instance variables of the
52handler. Subclasses should not need to override or extend the
53\method{__init__()} method.
54\end{classdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000055
Guido van Rossum9cb64801997-12-29 20:01:55 +000056
Fred Drakef9e1f651998-03-14 07:00:41 +000057\class{BaseHTTPRequestHandler} has the following instance variables:
Guido van Rossum9cb64801997-12-29 20:01:55 +000058
Fred Drakefc576191998-04-04 07:15:02 +000059\begin{memberdesc}{client_address}
Fred Drakef9e1f651998-03-14 07:00:41 +000060Contains a tuple of the form \code{(\var{host}, \var{port})} referring
61to the client's address.
Fred Drakefc576191998-04-04 07:15:02 +000062\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000063
Fred Drakefc576191998-04-04 07:15:02 +000064\begin{memberdesc}{command}
Fred Drakef9e1f651998-03-14 07:00:41 +000065Contains the command (request type). For example, \code{'GET'}.
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}{path}
Guido van Rossum9cb64801997-12-29 20:01:55 +000069Contains the request path.
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}{request_version}
Guido van Rossum9cb64801997-12-29 20:01:55 +000073Contains the version string from the request. For example,
Fred Drakef9e1f651998-03-14 07:00:41 +000074\code{'HTTP/1.0'}.
Fred Drakefc576191998-04-04 07:15:02 +000075\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000076
Fred Drakefc576191998-04-04 07:15:02 +000077\begin{memberdesc}{headers}
Fred Drakef9e1f651998-03-14 07:00:41 +000078Holds an instance of the class specified by the \member{MessageClass}
Guido van Rossum9cb64801997-12-29 20:01:55 +000079class variable. This instance parses and manages the headers in
80the HTTP request.
Fred Drakefc576191998-04-04 07:15:02 +000081\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000082
Fred Drakefc576191998-04-04 07:15:02 +000083\begin{memberdesc}{rfile}
Guido van Rossum9cb64801997-12-29 20:01:55 +000084Contains an input stream, positioned at the start of the optional
85input data.
Fred Drakefc576191998-04-04 07:15:02 +000086\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000087
Fred Drakefc576191998-04-04 07:15:02 +000088\begin{memberdesc}{wfile}
Guido van Rossum9cb64801997-12-29 20:01:55 +000089Contains the output stream for writing a response back to the client.
90Proper adherance to the HTTP protocol must be used when writing
91to this stream.
Fred Drakefc576191998-04-04 07:15:02 +000092\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +000093
Guido van Rossum9cb64801997-12-29 20:01:55 +000094
Fred Drakefc576191998-04-04 07:15:02 +000095\class{BaseHTTPRequestHandler} has the following class variables:
Guido van Rossum9cb64801997-12-29 20:01:55 +000096
Fred Drakefc576191998-04-04 07:15:02 +000097\begin{memberdesc}{server_version}
Guido van Rossum9cb64801997-12-29 20:01:55 +000098Specifies the server software version. You may want to override
99this.
100The format is multiple whitespace-separated strings,
101where each string is of the form name[/version].
Fred Drakef9e1f651998-03-14 07:00:41 +0000102For example, \code{'BaseHTTP/0.2'}.
Fred Drakefc576191998-04-04 07:15:02 +0000103\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000104
Fred Drakefc576191998-04-04 07:15:02 +0000105\begin{memberdesc}{sys_version}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000106Contains the Python system version, in a form usable by the
Fred Drakef9e1f651998-03-14 07:00:41 +0000107\member{version_string} method and the \member{server_version} class
108variable. For example, \code{'Python/1.4'}.
Fred Drakefc576191998-04-04 07:15:02 +0000109\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000110
Fred Drakefc576191998-04-04 07:15:02 +0000111\begin{memberdesc}{error_message_format}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000112Specifies a format string for building an error response to the
113client. It uses parenthesized, keyed format specifiers, so the
114format operand must be a dictionary. The \var{code} key should
115be an integer, specifing the numeric HTTP error code value.
116\var{message} should be a string containing a (detailed) error
117message of what occurred, and \var{explain} should be an
118explanation of the error code number. Default \var{message}
119and \var{explain} values can found in the \var{responses}
120class variable.
Fred Drakefc576191998-04-04 07:15:02 +0000121\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000122
Fred Drakefc576191998-04-04 07:15:02 +0000123\begin{memberdesc}{protocol_version}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000124This specifies the HTTP protocol version used in responses.
125Typically, this should not be overridden. Defaults to
Fred Drakef9e1f651998-03-14 07:00:41 +0000126\code{'HTTP/1.0'}.
Fred Drakefc576191998-04-04 07:15:02 +0000127\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000128
Fred Drakefc576191998-04-04 07:15:02 +0000129\begin{memberdesc}{MessageClass}
Fred Drakef9e1f651998-03-14 07:00:41 +0000130Specifies a \class{rfc822.Message}-like class to parse HTTP
131headers. Typically, this is not overridden, and it defaults to
132\class{mimetools.Message}.
133\withsubitem{(in module mimetools)}{\ttindex{Message}}
Fred Drakefc576191998-04-04 07:15:02 +0000134\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000135
Fred Drakefc576191998-04-04 07:15:02 +0000136\begin{memberdesc}{responses}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000137This variable contains a mapping of error code integers to two-element
138tuples containing a short and long message. For example,
Fred Drakef9e1f651998-03-14 07:00:41 +0000139\code{\{\var{code}: (\var{shortmessage}, \var{longmessage})\}}. The
Guido van Rossum9cb64801997-12-29 20:01:55 +0000140\var{shortmessage} is usually used as the \var{message} key in an
141error response, and \var{longmessage} as the \var{explain} key
Fred Drakef9e1f651998-03-14 07:00:41 +0000142(see the \member{error_message_format} class variable).
Fred Drakefc576191998-04-04 07:15:02 +0000143\end{memberdesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000144
Guido van Rossum9cb64801997-12-29 20:01:55 +0000145
Fred Drakef9e1f651998-03-14 07:00:41 +0000146A \class{BaseHTTPRequestHandler} instance has the following methods:
Guido van Rossum9cb64801997-12-29 20:01:55 +0000147
Fred Drakefc576191998-04-04 07:15:02 +0000148\begin{methoddesc}{handle}{}
Fred Drakef9e1f651998-03-14 07:00:41 +0000149Overrides the superclass' \method{handle()} method to provide the
Guido van Rossum9cb64801997-12-29 20:01:55 +0000150specific handler behavior. This method will parse and dispatch
Fred Drakefc576191998-04-04 07:15:02 +0000151the request to the appropriate \method{do_*()} method.
152\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000153
Fred Drakefc576191998-04-04 07:15:02 +0000154\begin{methoddesc}{send_error}{code\optional{, message}}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000155Sends and logs a complete error reply to the client. The numeric
156\var{code} specifies the HTTP error code, with \var{message} as
157optional, more specific text. A complete set of headers is sent,
Fred Drakef9e1f651998-03-14 07:00:41 +0000158followed by text composed using the \member{error_message_format}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000159class variable.
Fred Drakefc576191998-04-04 07:15:02 +0000160\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000161
Fred Drakefc576191998-04-04 07:15:02 +0000162\begin{methoddesc}{send_response}{code\optional{, message}}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000163Sends a response header and logs the accepted request. The HTTP
164response line is sent, followed by \emph{Server} and \emph{Date}
165headers. The values for these two headers are picked up from the
Fred Drakef9e1f651998-03-14 07:00:41 +0000166\method{version_string()} and \method{date_time_string()} methods,
Guido van Rossum9cb64801997-12-29 20:01:55 +0000167respectively.
Fred Drakefc576191998-04-04 07:15:02 +0000168\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000169
Fred Drakefc576191998-04-04 07:15:02 +0000170\begin{methoddesc}{send_header}{keyword, value}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000171Writes a specific MIME header to the output stream. \var{keyword}
172should specify the header keyword, with \var{value} specifying
173its value.
Fred Drakefc576191998-04-04 07:15:02 +0000174\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000175
Fred Drakefc576191998-04-04 07:15:02 +0000176\begin{methoddesc}{end_headers}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000177Sends a blank line, indicating the end of the MIME headers in
178the response.
Fred Drakefc576191998-04-04 07:15:02 +0000179\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000180
Fred Drakefc576191998-04-04 07:15:02 +0000181\begin{methoddesc}{log_request}{\optional{code\optional{, size}}}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000182Logs an accepted (successful) request. \var{code} should specify
183the numeric HTTP code associated with the response. If a size of
184the response is available, then it should be passed as the
185\var{size} parameter.
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}{log_error}{...}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000189Logs an error when a request cannot be fulfilled. By default,
Fred Drakef9e1f651998-03-14 07:00:41 +0000190it passes the message to \method{log_message()}, so it takes the
Guido van Rossum9cb64801997-12-29 20:01:55 +0000191same arguments (\var{format} and additional values).
Fred Drakefc576191998-04-04 07:15:02 +0000192\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000193
Fred Drakefc576191998-04-04 07:15:02 +0000194\begin{methoddesc}{log_message}{format, ...}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000195Logs an arbitrary message to \code{sys.stderr}. This is typically
196overridden to create custom error logging mechanisms. The
197\var{format} argument is a standard printf-style format string,
Fred Drakef9e1f651998-03-14 07:00:41 +0000198where the additional arguments to \method{log_message()} are applied
Guido van Rossum9cb64801997-12-29 20:01:55 +0000199as inputs to the formatting. The client address and current date
200and time are prefixed to every message logged.
Fred Drakefc576191998-04-04 07:15:02 +0000201\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000202
Fred Drakefc576191998-04-04 07:15:02 +0000203\begin{methoddesc}{version_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000204Returns the server software's version string. This is a combination
Fred Drakef9e1f651998-03-14 07:00:41 +0000205of the \member{server_version} and \member{sys_version} class variables.
Fred Drakefc576191998-04-04 07:15:02 +0000206\end{methoddesc}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000207
Fred Drakefc576191998-04-04 07:15:02 +0000208\begin{methoddesc}{date_time_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000209Returns the current date and time, formatted for a message header.
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}{log_data_time_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000213Returns the current date and time, formatted for logging.
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}{address_string}{}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000217Returns the client address, formatted for logging. A name lookup
218is performed on the client's IP address.
Fred Drakefc576191998-04-04 07:15:02 +0000219\end{methoddesc}