blob: b9e2edb371697cbfaf23d3d3f60bd66787245e60 [file] [log] [blame]
Guido van Rossum9cb64801997-12-29 20:01:55 +00001\section{Standard Module \sectcode{BaseHTTPServer}}
2\label{module-BaseHTTPServer}
3\stmodindex{BaseHTTPServer}
4
5\indexii{WWW}{server}
6\indexii{HTTP}{protocol}
7\index{URL}
8\index{httpd}
9
10\renewcommand{\indexsubitem}{(in module BaseHTTPServer)}
11
12This module defines two classes for implementing HTTP servers
13(web servers). Usually, this module isn't used directly, but is used
14as a basis for building functioning web servers. See the
15\code{SimpleHTTPServer} and \code{CGIHTTPServer} modules.
16\stmodindex{SimpleHTTPServer}
17\stmodindex{CGIHTTPServer}
18
19The first class, \code{HTTPServer}, is a \code{SocketServer.TCPServer}
20subclass. It creates and listens at the web socket, dispatching the
21requests to a handler. Code to create and run the server looks like
22this:
23
24\bcode\begin{verbatim}
25def run(server_class=BaseHTTPServer.HTTPServer,
26 handler_class=BaseHTTPServer.BaseHTTPRequestHandler):
27 server_address = ('', 8000)
28 httpd = server_class(server_address, handler_class)
29 httpd.serve_forever()
30\end{verbatim}\ecode
31%
32The \code{HTTPServer} class builds on the \code{TCPServer} class by
33storing the server address as instance
34variables named \code{server_name} and \code{server_port}. The
35server is accessible by the handler, typically through the handler's
36\code{server} instance variable.
37
38The module's second class, \code{BaseHTTPRequestHandler}, is used
39to handle the HTTP requests that arrive at the server. By itself,
40it cannot respond to any actual HTTP requests; it must be subclassed
41to handle each request method (e.g. GET or POST).
42\code{BaseHTTPRequestHandler} provides a number of class and instance
43variables, and methods for use by subclasses.
44
45The handler will parse the request and the headers, then call a
46method specific to the request type. The method name is constructed
47from the request. For example, for the request \code{SPAM}, the
48\code{do_SPAM} method will be called with no arguments. All of
49the relevant information is stored into instance variables of the
50handler.
51
52\renewcommand{\indexsubitem}{(BaseHTTPRequestHandler instance variables)}
53
54\code{BaseHTTPRequestHandler} has the following instance variables:
55
56\begin{datadesc}{client_address}
57Contains a tuple of the form (host, port) referring to the client's
58address.
59\end{datadesc}
60
61\begin{datadesc}{command}
62Contains the command (request type). For example, \code{"GET"}.
63\end{datadesc}
64
65\begin{datadesc}{path}
66Contains the request path.
67\end{datadesc}
68
69\begin{datadesc}{request_version}
70Contains the version string from the request. For example,
71\code{"HTTP/1.0"}.
72\end{datadesc}
73
74\begin{datadesc}{headers}
75Holds an instance of the class specified by the \var{MessageClass}
76class variable. This instance parses and manages the headers in
77the HTTP request.
78\end{datadesc}
79
80\begin{datadesc}{rfile}
81Contains an input stream, positioned at the start of the optional
82input data.
83\end{datadesc}
84
85\begin{datadesc}{wfile}
86Contains the output stream for writing a response back to the client.
87Proper adherance to the HTTP protocol must be used when writing
88to this stream.
89\end{datadesc}
90
91\renewcommand{\indexsubitem}{(BaseHTTPRequestHandler class variables)}
92
93\code{BaseHTTPRequestHandler} has the following class variables:
94
95\begin{datadesc}{server_version}
96Specifies the server software version. You may want to override
97this.
98The format is multiple whitespace-separated strings,
99where each string is of the form name[/version].
100For example, \code{"BaseHTTP/0.2"}.
101\end{datadesc}
102
103\begin{datadesc}{sys_version}
104Contains the Python system version, in a form usable by the
105\code{version_string} method and the \code{server_version} class
106variable. For example, \code{"Python/1.4"}.
107\end{datadesc}
108
109\begin{datadesc}{error_message_format}
110Specifies a format string for building an error response to the
111client. It uses parenthesized, keyed format specifiers, so the
112format operand must be a dictionary. The \var{code} key should
113be an integer, specifing the numeric HTTP error code value.
114\var{message} should be a string containing a (detailed) error
115message of what occurred, and \var{explain} should be an
116explanation of the error code number. Default \var{message}
117and \var{explain} values can found in the \var{responses}
118class variable.
119\end{datadesc}
120
121\begin{datadesc}{protocol_version}
122This specifies the HTTP protocol version used in responses.
123Typically, this should not be overridden. Defaults to
124\code{"HTTP/1.0"}.
125\end{datadesc}
126
127\begin{datadesc}{MessageClass}
128Specifies a Message-like class to parse HTTP headers. Typically,
129this is not overridden, and it defaults to \code{mimetools.Message}.
130\end{datadesc}
131
132\begin{datadesc}{responses}
133This variable contains a mapping of error code integers to two-element
134tuples containing a short and long message. For example,
135\code{\{code : (shortmessage, longmessage)\}}. The
136\var{shortmessage} is usually used as the \var{message} key in an
137error response, and \var{longmessage} as the \var{explain} key
138(see the \code{error_message_format} class variable).
139\end{datadesc}
140
141\renewcommand{\indexsubitem}{(BaseHTTPRequestHandler method)}
142
143A \code{BaseHTTPRequestHandler} instance has the following methods:
144
145\begin{funcdesc}{handle}{}
146Overrides the superclass' \code{handle} method to provide the
147specific handler behavior. This method will parse and dispatch
148the request to the appropriate \code{do_}* method.
149\end{funcdesc}
150
151\begin{funcdesc}{send_error}{code\optional{\, message}}
152Sends and logs a complete error reply to the client. The numeric
153\var{code} specifies the HTTP error code, with \var{message} as
154optional, more specific text. A complete set of headers is sent,
155followed by text composed using the \code{error_message_format}
156class variable.
157\end{funcdesc}
158
159\begin{funcdesc}{send_response}{code\optional{\, message}}
160Sends a response header and logs the accepted request. The HTTP
161response line is sent, followed by \emph{Server} and \emph{Date}
162headers. The values for these two headers are picked up from the
163\code{version_string()} and \code{date_time_string()} methods,
164respectively.
165\end{funcdesc}
166
167\begin{funcdesc}{send_header}{keyword\, value}
168Writes a specific MIME header to the output stream. \var{keyword}
169should specify the header keyword, with \var{value} specifying
170its value.
171\end{funcdesc}
172
173\begin{funcdesc}{end_headers}{}
174Sends a blank line, indicating the end of the MIME headers in
175the response.
176\end{funcdesc}
177
178\begin{funcdesc}{log_request}{\optional{code\optional{\, size}}}
179Logs an accepted (successful) request. \var{code} should specify
180the numeric HTTP code associated with the response. If a size of
181the response is available, then it should be passed as the
182\var{size} parameter.
183\end{funcdesc}
184
185\begin{funcdesc}{log_error}{...}
186Logs an error when a request cannot be fulfilled. By default,
187it passes the message to \code{log_message}, so it takes the
188same arguments (\var{format} and additional values).
189\end{funcdesc}
190
191\begin{funcdesc}{log_message}{format, ...}
192Logs an arbitrary message to \code{sys.stderr}. This is typically
193overridden to create custom error logging mechanisms. The
194\var{format} argument is a standard printf-style format string,
195where the additional arguments to \code{log_message} are applied
196as inputs to the formatting. The client address and current date
197and time are prefixed to every message logged.
198\end{funcdesc}
199
200\begin{funcdesc}{version_string}{}
201Returns the server software's version string. This is a combination
202of the \var{server_version} and \var{sys_version} class variables.
203\end{funcdesc}
204
205\begin{funcdesc}{date_time_string}{}
206Returns the current date and time, formatted for a message header.
207\end{funcdesc}
208
209\begin{funcdesc}{log_data_time_string}{}
210Returns the current date and time, formatted for logging.
211\end{funcdesc}
212
213\begin{funcdesc}{address_string}{}
214Returns the client address, formatted for logging. A name lookup
215is performed on the client's IP address.
216\end{funcdesc}