blob: 12d94834c5d7698b5b6112801810965c6d1a085c [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
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):
26 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 Drakef9e1f651998-03-14 07:00:41 +000031The \class{HTTPServer} class builds on the \class{TCPServer} class by
32storing the server address as instance
33variables named \member{server_name} and \member{server_port}. The
34server is accessible by the handler, typically through the handler's
35\member{server} instance variable.
36
37The module's second class, \class{BaseHTTPRequestHandler}, is used
Guido van Rossum9cb64801997-12-29 20:01:55 +000038to handle the HTTP requests that arrive at the server. By itself,
39it cannot respond to any actual HTTP requests; it must be subclassed
40to handle each request method (e.g. GET or POST).
Fred Drakef9e1f651998-03-14 07:00:41 +000041\class{BaseHTTPRequestHandler} provides a number of class and instance
Guido van Rossum9cb64801997-12-29 20:01:55 +000042variables, and methods for use by subclasses.
43
44The handler will parse the request and the headers, then call a
45method specific to the request type. The method name is constructed
Fred Drakef9e1f651998-03-14 07:00:41 +000046from the request. For example, for the request \samp{SPAM}, the
47\method{do_SPAM()} method will be called with no arguments. All of
Guido van Rossum9cb64801997-12-29 20:01:55 +000048the relevant information is stored into instance variables of the
49handler.
50
Fred Drakef9e1f651998-03-14 07:00:41 +000051\setindexsubitem{(BaseHTTPRequestHandler attribute)}
Guido van Rossum9cb64801997-12-29 20:01:55 +000052
Fred Drakef9e1f651998-03-14 07:00:41 +000053\class{BaseHTTPRequestHandler} has the following instance variables:
Guido van Rossum9cb64801997-12-29 20:01:55 +000054
55\begin{datadesc}{client_address}
Fred Drakef9e1f651998-03-14 07:00:41 +000056Contains a tuple of the form \code{(\var{host}, \var{port})} referring
57to the client's address.
Guido van Rossum9cb64801997-12-29 20:01:55 +000058\end{datadesc}
59
60\begin{datadesc}{command}
Fred Drakef9e1f651998-03-14 07:00:41 +000061Contains the command (request type). For example, \code{'GET'}.
Guido van Rossum9cb64801997-12-29 20:01:55 +000062\end{datadesc}
63
64\begin{datadesc}{path}
65Contains the request path.
66\end{datadesc}
67
68\begin{datadesc}{request_version}
69Contains the version string from the request. For example,
Fred Drakef9e1f651998-03-14 07:00:41 +000070\code{'HTTP/1.0'}.
Guido van Rossum9cb64801997-12-29 20:01:55 +000071\end{datadesc}
72
73\begin{datadesc}{headers}
Fred Drakef9e1f651998-03-14 07:00:41 +000074Holds an instance of the class specified by the \member{MessageClass}
Guido van Rossum9cb64801997-12-29 20:01:55 +000075class variable. This instance parses and manages the headers in
76the HTTP request.
77\end{datadesc}
78
79\begin{datadesc}{rfile}
80Contains an input stream, positioned at the start of the optional
81input data.
82\end{datadesc}
83
84\begin{datadesc}{wfile}
85Contains the output stream for writing a response back to the client.
86Proper adherance to the HTTP protocol must be used when writing
87to this stream.
88\end{datadesc}
89
Fred Drakef9e1f651998-03-14 07:00:41 +000090\setindexsubitem{(BaseHTTPRequestHandler attribute)}
Guido van Rossum9cb64801997-12-29 20:01:55 +000091
92\code{BaseHTTPRequestHandler} has the following class variables:
93
94\begin{datadesc}{server_version}
95Specifies the server software version. You may want to override
96this.
97The format is multiple whitespace-separated strings,
98where each string is of the form name[/version].
Fred Drakef9e1f651998-03-14 07:00:41 +000099For example, \code{'BaseHTTP/0.2'}.
Guido van Rossum9cb64801997-12-29 20:01:55 +0000100\end{datadesc}
101
102\begin{datadesc}{sys_version}
103Contains the Python system version, in a form usable by the
Fred Drakef9e1f651998-03-14 07:00:41 +0000104\member{version_string} method and the \member{server_version} class
105variable. For example, \code{'Python/1.4'}.
Guido van Rossum9cb64801997-12-29 20:01:55 +0000106\end{datadesc}
107
108\begin{datadesc}{error_message_format}
109Specifies a format string for building an error response to the
110client. It uses parenthesized, keyed format specifiers, so the
111format operand must be a dictionary. The \var{code} key should
112be an integer, specifing the numeric HTTP error code value.
113\var{message} should be a string containing a (detailed) error
114message of what occurred, and \var{explain} should be an
115explanation of the error code number. Default \var{message}
116and \var{explain} values can found in the \var{responses}
117class variable.
118\end{datadesc}
119
120\begin{datadesc}{protocol_version}
121This specifies the HTTP protocol version used in responses.
122Typically, this should not be overridden. Defaults to
Fred Drakef9e1f651998-03-14 07:00:41 +0000123\code{'HTTP/1.0'}.
Guido van Rossum9cb64801997-12-29 20:01:55 +0000124\end{datadesc}
125
126\begin{datadesc}{MessageClass}
Fred Drakef9e1f651998-03-14 07:00:41 +0000127Specifies a \class{rfc822.Message}-like class to parse HTTP
128headers. Typically, this is not overridden, and it defaults to
129\class{mimetools.Message}.
130\withsubitem{(in module mimetools)}{\ttindex{Message}}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000131\end{datadesc}
132
133\begin{datadesc}{responses}
134This variable contains a mapping of error code integers to two-element
135tuples containing a short and long message. For example,
Fred Drakef9e1f651998-03-14 07:00:41 +0000136\code{\{\var{code}: (\var{shortmessage}, \var{longmessage})\}}. The
Guido van Rossum9cb64801997-12-29 20:01:55 +0000137\var{shortmessage} is usually used as the \var{message} key in an
138error response, and \var{longmessage} as the \var{explain} key
Fred Drakef9e1f651998-03-14 07:00:41 +0000139(see the \member{error_message_format} class variable).
Guido van Rossum9cb64801997-12-29 20:01:55 +0000140\end{datadesc}
141
Fred Drake19479911998-02-13 06:58:54 +0000142\setindexsubitem{(BaseHTTPRequestHandler method)}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000143
Fred Drakef9e1f651998-03-14 07:00:41 +0000144A \class{BaseHTTPRequestHandler} instance has the following methods:
Guido van Rossum9cb64801997-12-29 20:01:55 +0000145
146\begin{funcdesc}{handle}{}
Fred Drakef9e1f651998-03-14 07:00:41 +0000147Overrides the superclass' \method{handle()} method to provide the
Guido van Rossum9cb64801997-12-29 20:01:55 +0000148specific handler behavior. This method will parse and dispatch
Fred Drakef9e1f651998-03-14 07:00:41 +0000149the request to the appropriate \code{do_*()} method.
Guido van Rossum9cb64801997-12-29 20:01:55 +0000150\end{funcdesc}
151
Fred Drakef9e1f651998-03-14 07:00:41 +0000152\begin{funcdesc}{send_error}{code\optional{, message}}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000153Sends and logs a complete error reply to the client. The numeric
154\var{code} specifies the HTTP error code, with \var{message} as
155optional, more specific text. A complete set of headers is sent,
Fred Drakef9e1f651998-03-14 07:00:41 +0000156followed by text composed using the \member{error_message_format}
Guido van Rossum9cb64801997-12-29 20:01:55 +0000157class variable.
158\end{funcdesc}
159
160\begin{funcdesc}{send_response}{code\optional{\, message}}
161Sends a response header and logs the accepted request. The HTTP
162response line is sent, followed by \emph{Server} and \emph{Date}
163headers. The values for these two headers are picked up from the
Fred Drakef9e1f651998-03-14 07:00:41 +0000164\method{version_string()} and \method{date_time_string()} methods,
Guido van Rossum9cb64801997-12-29 20:01:55 +0000165respectively.
166\end{funcdesc}
167
168\begin{funcdesc}{send_header}{keyword\, value}
169Writes a specific MIME header to the output stream. \var{keyword}
170should specify the header keyword, with \var{value} specifying
171its value.
172\end{funcdesc}
173
174\begin{funcdesc}{end_headers}{}
175Sends a blank line, indicating the end of the MIME headers in
176the response.
177\end{funcdesc}
178
179\begin{funcdesc}{log_request}{\optional{code\optional{\, size}}}
180Logs an accepted (successful) request. \var{code} should specify
181the numeric HTTP code associated with the response. If a size of
182the response is available, then it should be passed as the
183\var{size} parameter.
184\end{funcdesc}
185
186\begin{funcdesc}{log_error}{...}
187Logs an error when a request cannot be fulfilled. By default,
Fred Drakef9e1f651998-03-14 07:00:41 +0000188it passes the message to \method{log_message()}, so it takes the
Guido van Rossum9cb64801997-12-29 20:01:55 +0000189same arguments (\var{format} and additional values).
190\end{funcdesc}
191
192\begin{funcdesc}{log_message}{format, ...}
193Logs an arbitrary message to \code{sys.stderr}. This is typically
194overridden to create custom error logging mechanisms. The
195\var{format} argument is a standard printf-style format string,
Fred Drakef9e1f651998-03-14 07:00:41 +0000196where the additional arguments to \method{log_message()} are applied
Guido van Rossum9cb64801997-12-29 20:01:55 +0000197as inputs to the formatting. The client address and current date
198and time are prefixed to every message logged.
199\end{funcdesc}
200
201\begin{funcdesc}{version_string}{}
202Returns the server software's version string. This is a combination
Fred Drakef9e1f651998-03-14 07:00:41 +0000203of the \member{server_version} and \member{sys_version} class variables.
Guido van Rossum9cb64801997-12-29 20:01:55 +0000204\end{funcdesc}
205
206\begin{funcdesc}{date_time_string}{}
207Returns the current date and time, formatted for a message header.
208\end{funcdesc}
209
210\begin{funcdesc}{log_data_time_string}{}
211Returns the current date and time, formatted for logging.
212\end{funcdesc}
213
214\begin{funcdesc}{address_string}{}
215Returns the client address, formatted for logging. A name lookup
216is performed on the client's IP address.
217\end{funcdesc}