Initial commit of OpenSPARC T2 architecture model.
[OpenSPARC-T2-SAM] / sam-t2 / devtools / v8plus / html / python / lib / module-BaseHTTPServer.html
CommitLineData
920dae64
AT
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
2<html>
3<head>
4<link rel="STYLESHEET" href="lib.css" type='text/css' />
5<link rel="SHORTCUT ICON" href="../icons/pyfav.png" type="image/png" />
6<link rel='start' href='../index.html' title='Python Documentation Index' />
7<link rel="first" href="lib.html" title='Python Library Reference' />
8<link rel='contents' href='contents.html' title="Contents" />
9<link rel='index' href='genindex.html' title='Index' />
10<link rel='last' href='about.html' title='About this document...' />
11<link rel='help' href='about.html' title='About this document...' />
12<link rel="next" href="module-SimpleHTTPServer.html" />
13<link rel="prev" href="module-SocketServer.html" />
14<link rel="parent" href="internet.html" />
15<link rel="next" href="module-SimpleHTTPServer.html" />
16<meta name='aesop' content='information' />
17<title>11.17 BaseHTTPServer -- Basic HTTP server</title>
18</head>
19<body>
20<DIV CLASS="navigation">
21<div id='top-navigation-panel' xml:id='top-navigation-panel'>
22<table align="center" width="100%" cellpadding="0" cellspacing="2">
23<tr>
24<td class='online-navigation'><a rel="prev" title="11.16.3 RequestHandler Objects"
25 href="node537.html"><img src='../icons/previous.png'
26 border='0' height='32' alt='Previous Page' width='32' /></A></td>
27<td class='online-navigation'><a rel="parent" title="11. Internet Protocols and"
28 href="internet.html"><img src='../icons/up.png'
29 border='0' height='32' alt='Up One Level' width='32' /></A></td>
30<td class='online-navigation'><a rel="next" title="11.18 SimpleHTTPServer "
31 href="module-SimpleHTTPServer.html"><img src='../icons/next.png'
32 border='0' height='32' alt='Next Page' width='32' /></A></td>
33<td align="center" width="100%">Python Library Reference</td>
34<td class='online-navigation'><a rel="contents" title="Table of Contents"
35 href="contents.html"><img src='../icons/contents.png'
36 border='0' height='32' alt='Contents' width='32' /></A></td>
37<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
38 border='0' height='32' alt='Module Index' width='32' /></a></td>
39<td class='online-navigation'><a rel="index" title="Index"
40 href="genindex.html"><img src='../icons/index.png'
41 border='0' height='32' alt='Index' width='32' /></A></td>
42</tr></table>
43<div class='online-navigation'>
44<b class="navlabel">Previous:</b>
45<a class="sectref" rel="prev" href="node537.html">11.16.3 RequestHandler Objects</A>
46<b class="navlabel">Up:</b>
47<a class="sectref" rel="parent" href="internet.html">11. Internet Protocols and</A>
48<b class="navlabel">Next:</b>
49<a class="sectref" rel="next" href="module-SimpleHTTPServer.html">11.18 SimpleHTTPServer </A>
50</div>
51<hr /></div>
52</DIV>
53<!--End of Navigation Panel-->
54
55<H1><A NAME="SECTION00131700000000000000000">
5611.17 <tt class="module">BaseHTTPServer</tt> --
57 Basic HTTP server</A>
58</H1>
59
60<P>
61<A NAME="module-BaseHTTPServer"></A>
62
63<P>
64<a id='l2h-3569' xml:id='l2h-3569'></a><a id='l2h-3570' xml:id='l2h-3570'></a><a id='l2h-3601' xml:id='l2h-3601'></a>
65
66<P>
67This module defines two classes for implementing HTTP servers
68(Web servers). Usually, this module isn't used directly, but is used
69as a basis for building functioning Web servers. See the
70<tt class="module"><a href="module-SimpleHTTPServer.html">SimpleHTTPServer</a></tt><a id='l2h-3602' xml:id='l2h-3602'></a> and
71<tt class="module"><a href="module-CGIHTTPServer.html">CGIHTTPServer</a></tt><a id='l2h-3603' xml:id='l2h-3603'></a> modules.
72
73<P>
74The first class, <tt class="class">HTTPServer</tt>, is a
75<tt class="class">SocketServer.TCPServer</tt> subclass. It creates and listens at the
76HTTP socket, dispatching the requests to a handler. Code to create and
77run the server looks like this:
78
79<P>
80<div class="verbatim"><pre>
81def run(server_class=BaseHTTPServer.HTTPServer,
82 handler_class=BaseHTTPServer.BaseHTTPRequestHandler):
83 server_address = ('', 8000)
84 httpd = server_class(server_address, handler_class)
85 httpd.serve_forever()
86</pre></div>
87
88<P>
89<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
90 <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-3571' xml:id='l2h-3571' class="class">HTTPServer</tt></b>(</nobr></td>
91 <td><var>server_address, RequestHandlerClass</var>)</td></tr></table></dt>
92<dd>
93This class builds on the <tt class="class">TCPServer</tt> class by
94storing the server address as instance
95variables named <tt class="member">server_name</tt> and <tt class="member">server_port</tt>. The
96server is accessible by the handler, typically through the handler's
97<tt class="member">server</tt> instance variable.
98</dl>
99
100<P>
101<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
102 <td><nobr><b><span class="typelabel">class</span>&nbsp;<tt id='l2h-3572' xml:id='l2h-3572' class="class">BaseHTTPRequestHandler</tt></b>(</nobr></td>
103 <td><var>request, client_address, server</var>)</td></tr></table></dt>
104<dd>
105This class is used
106to handle the HTTP requests that arrive at the server. By itself,
107it cannot respond to any actual HTTP requests; it must be subclassed
108to handle each request method (e.g. GET or POST).
109<tt class="class">BaseHTTPRequestHandler</tt> provides a number of class and instance
110variables, and methods for use by subclasses.
111
112<P>
113The handler will parse the request and the headers, then call a
114method specific to the request type. The method name is constructed
115from the request. For example, for the request method "<tt class="samp">SPAM</tt>", the
116<tt class="method">do_SPAM()</tt> method will be called with no arguments. All of
117the relevant information is stored in instance variables of the
118handler. Subclasses should not need to override or extend the
119<tt class="method">__init__()</tt> method.
120</dl>
121
122<P>
123<tt class="class">BaseHTTPRequestHandler</tt> has the following instance variables:
124
125<P>
126<dl><dt><b><tt id='l2h-3573' xml:id='l2h-3573' class="member">client_address</tt></b></dt>
127<dd>
128Contains a tuple of the form <code>(<var>host</var>, <var>port</var>)</code> referring
129to the client's address.
130</dl>
131
132<P>
133<dl><dt><b><tt id='l2h-3574' xml:id='l2h-3574' class="member">command</tt></b></dt>
134<dd>
135Contains the command (request type). For example, <code>'GET'</code>.
136</dl>
137
138<P>
139<dl><dt><b><tt id='l2h-3575' xml:id='l2h-3575' class="member">path</tt></b></dt>
140<dd>
141Contains the request path.
142</dl>
143
144<P>
145<dl><dt><b><tt id='l2h-3576' xml:id='l2h-3576' class="member">request_version</tt></b></dt>
146<dd>
147Contains the version string from the request. For example,
148<code>'HTTP/1.0'</code>.
149</dl>
150
151<P>
152<dl><dt><b><tt id='l2h-3577' xml:id='l2h-3577' class="member">headers</tt></b></dt>
153<dd>
154Holds an instance of the class specified by the <tt class="member">MessageClass</tt>
155class variable. This instance parses and manages the headers in
156the HTTP request.
157</dl>
158
159<P>
160<dl><dt><b><tt id='l2h-3578' xml:id='l2h-3578' class="member">rfile</tt></b></dt>
161<dd>
162Contains an input stream, positioned at the start of the optional
163input data.
164</dl>
165
166<P>
167<dl><dt><b><tt id='l2h-3579' xml:id='l2h-3579' class="member">wfile</tt></b></dt>
168<dd>
169Contains the output stream for writing a response back to the client.
170Proper adherence to the HTTP protocol must be used when writing
171to this stream.
172</dl>
173
174<P>
175<tt class="class">BaseHTTPRequestHandler</tt> has the following class variables:
176
177<P>
178<dl><dt><b><tt id='l2h-3580' xml:id='l2h-3580' class="member">server_version</tt></b></dt>
179<dd>
180Specifies the server software version. You may want to override
181this.
182The format is multiple whitespace-separated strings,
183where each string is of the form name[/version].
184For example, <code>'BaseHTTP/0.2'</code>.
185</dl>
186
187<P>
188<dl><dt><b><tt id='l2h-3581' xml:id='l2h-3581' class="member">sys_version</tt></b></dt>
189<dd>
190Contains the Python system version, in a form usable by the
191<tt class="member">version_string</tt> method and the <tt class="member">server_version</tt> class
192variable. For example, <code>'Python/1.4'</code>.
193</dl>
194
195<P>
196<dl><dt><b><tt id='l2h-3582' xml:id='l2h-3582' class="member">error_message_format</tt></b></dt>
197<dd>
198Specifies a format string for building an error response to the
199client. It uses parenthesized, keyed format specifiers, so the
200format operand must be a dictionary. The <var>code</var> key should
201be an integer, specifying the numeric HTTP error code value.
202<var>message</var> should be a string containing a (detailed) error
203message of what occurred, and <var>explain</var> should be an
204explanation of the error code number. Default <var>message</var>
205and <var>explain</var> values can found in the <var>responses</var>
206class variable.
207</dl>
208
209<P>
210<dl><dt><b><tt id='l2h-3583' xml:id='l2h-3583' class="member">protocol_version</tt></b></dt>
211<dd>
212This specifies the HTTP protocol version used in responses. If set
213to <code>'HTTP/1.1'</code>, the server will permit HTTP persistent
214connections; however, your server <em>must</em> then include an
215accurate <code>Content-Length</code> header (using <tt class="method">send_header()</tt>)
216in all of its responses to clients. For backwards compatibility,
217the setting defaults to <code>'HTTP/1.0'</code>.
218</dl>
219
220<P>
221<dl><dt><b><tt id='l2h-3584' xml:id='l2h-3584' class="member">MessageClass</tt></b></dt>
222<dd>
223Specifies a <tt class="class">rfc822.Message</tt>-like class to parse HTTP
224headers. Typically, this is not overridden, and it defaults to
225<tt class="class">mimetools.Message</tt>.
226<a id='l2h-3586' xml:id='l2h-3586'></a></dl>
227
228<P>
229<dl><dt><b><tt id='l2h-3587' xml:id='l2h-3587' class="member">responses</tt></b></dt>
230<dd>
231This variable contains a mapping of error code integers to two-element
232tuples containing a short and long message. For example,
233<code>{<var>code</var>: (<var>shortmessage</var>, <var>longmessage</var>)}</code>. The
234<var>shortmessage</var> is usually used as the <var>message</var> key in an
235error response, and <var>longmessage</var> as the <var>explain</var> key
236(see the <tt class="member">error_message_format</tt> class variable).
237</dl>
238
239<P>
240A <tt class="class">BaseHTTPRequestHandler</tt> instance has the following methods:
241
242<P>
243<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
244 <td><nobr><b><tt id='l2h-3588' xml:id='l2h-3588' class="method">handle</tt></b>(</nobr></td>
245 <td><var></var>)</td></tr></table></dt>
246<dd>
247Calls <tt class="method">handle_one_request()</tt> once (or, if persistent connections
248are enabled, multiple times) to handle incoming HTTP requests.
249You should never need to override it; instead, implement appropriate
250<tt class="method">do_*()</tt> methods.
251</dl>
252
253<P>
254<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
255 <td><nobr><b><tt id='l2h-3589' xml:id='l2h-3589' class="method">handle_one_request</tt></b>(</nobr></td>
256 <td><var></var>)</td></tr></table></dt>
257<dd>
258This method will parse and dispatch
259the request to the appropriate <tt class="method">do_*()</tt> method. You should
260never need to override it.
261</dl>
262
263<P>
264<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
265 <td><nobr><b><tt id='l2h-3590' xml:id='l2h-3590' class="method">send_error</tt></b>(</nobr></td>
266 <td><var>code</var><big>[</big><var>, message</var><big>]</big><var></var>)</td></tr></table></dt>
267<dd>
268Sends and logs a complete error reply to the client. The numeric
269<var>code</var> specifies the HTTP error code, with <var>message</var> as
270optional, more specific text. A complete set of headers is sent,
271followed by text composed using the <tt class="member">error_message_format</tt>
272class variable.
273</dl>
274
275<P>
276<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
277 <td><nobr><b><tt id='l2h-3591' xml:id='l2h-3591' class="method">send_response</tt></b>(</nobr></td>
278 <td><var>code</var><big>[</big><var>, message</var><big>]</big><var></var>)</td></tr></table></dt>
279<dd>
280Sends a response header and logs the accepted request. The HTTP
281response line is sent, followed by <em>Server</em> and <em>Date</em>
282headers. The values for these two headers are picked up from the
283<tt class="method">version_string()</tt> and <tt class="method">date_time_string()</tt> methods,
284respectively.
285</dl>
286
287<P>
288<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
289 <td><nobr><b><tt id='l2h-3592' xml:id='l2h-3592' class="method">send_header</tt></b>(</nobr></td>
290 <td><var>keyword, value</var>)</td></tr></table></dt>
291<dd>
292Writes a specific HTTP header to the output stream. <var>keyword</var>
293should specify the header keyword, with <var>value</var> specifying
294its value.
295</dl>
296
297<P>
298<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
299 <td><nobr><b><tt id='l2h-3593' xml:id='l2h-3593' class="method">end_headers</tt></b>(</nobr></td>
300 <td><var></var>)</td></tr></table></dt>
301<dd>
302Sends a blank line, indicating the end of the HTTP headers in
303the response.
304</dl>
305
306<P>
307<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
308 <td><nobr><b><tt id='l2h-3594' xml:id='l2h-3594' class="method">log_request</tt></b>(</nobr></td>
309 <td><var></var><big>[</big><var>code</var><big>[</big><var>, size</var><big>]</big><var></var><big>]</big><var></var>)</td></tr></table></dt>
310<dd>
311Logs an accepted (successful) request. <var>code</var> should specify
312the numeric HTTP code associated with the response. If a size of
313the response is available, then it should be passed as the
314<var>size</var> parameter.
315</dl>
316
317<P>
318<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
319 <td><nobr><b><tt id='l2h-3595' xml:id='l2h-3595' class="method">log_error</tt></b>(</nobr></td>
320 <td><var>...</var>)</td></tr></table></dt>
321<dd>
322Logs an error when a request cannot be fulfilled. By default,
323it passes the message to <tt class="method">log_message()</tt>, so it takes the
324same arguments (<var>format</var> and additional values).
325</dl>
326
327<P>
328<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
329 <td><nobr><b><tt id='l2h-3596' xml:id='l2h-3596' class="method">log_message</tt></b>(</nobr></td>
330 <td><var>format, ...</var>)</td></tr></table></dt>
331<dd>
332Logs an arbitrary message to <code>sys.stderr</code>. This is typically
333overridden to create custom error logging mechanisms. The
334<var>format</var> argument is a standard printf-style format string,
335where the additional arguments to <tt class="method">log_message()</tt> are applied
336as inputs to the formatting. The client address and current date
337and time are prefixed to every message logged.
338</dl>
339
340<P>
341<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
342 <td><nobr><b><tt id='l2h-3597' xml:id='l2h-3597' class="method">version_string</tt></b>(</nobr></td>
343 <td><var></var>)</td></tr></table></dt>
344<dd>
345Returns the server software's version string. This is a combination
346of the <tt class="member">server_version</tt> and <tt class="member">sys_version</tt> class variables.
347</dl>
348
349<P>
350<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
351 <td><nobr><b><tt id='l2h-3598' xml:id='l2h-3598' class="method">date_time_string</tt></b>(</nobr></td>
352 <td><var></var>)</td></tr></table></dt>
353<dd>
354Returns the current date and time, formatted for a message header.
355</dl>
356
357<P>
358<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
359 <td><nobr><b><tt id='l2h-3599' xml:id='l2h-3599' class="method">log_data_time_string</tt></b>(</nobr></td>
360 <td><var></var>)</td></tr></table></dt>
361<dd>
362Returns the current date and time, formatted for logging.
363</dl>
364
365<P>
366<dl><dt><table cellpadding="0" cellspacing="0"><tr valign="baseline">
367 <td><nobr><b><tt id='l2h-3600' xml:id='l2h-3600' class="method">address_string</tt></b>(</nobr></td>
368 <td><var></var>)</td></tr></table></dt>
369<dd>
370Returns the client address, formatted for logging. A name lookup
371is performed on the client's IP address.
372</dl>
373
374<P>
375<div class="seealso">
376 <p class="heading">See Also:</p>
377
378 <dl compact="compact" class="seemodule">
379 <dt>Module <b><tt class="module"><a href="module-CGIHTTPServer.html">CGIHTTPServer</a></tt>:</b>
380 <dd>Extended request handler that supports CGI
381 scripts.
382 </dl>
383
384<P>
385<dl compact="compact" class="seemodule">
386 <dt>Module <b><tt class="module"><a href="module-SimpleHTTPServer.html">SimpleHTTPServer</a></tt>:</b>
387 <dd>Basic request handler that limits response
388 to files actually under the document root.
389 </dl>
390</div>
391
392<DIV CLASS="navigation">
393<div class='online-navigation'>
394<p></p><hr />
395<table align="center" width="100%" cellpadding="0" cellspacing="2">
396<tr>
397<td class='online-navigation'><a rel="prev" title="11.16.3 RequestHandler Objects"
398 href="node537.html"><img src='../icons/previous.png'
399 border='0' height='32' alt='Previous Page' width='32' /></A></td>
400<td class='online-navigation'><a rel="parent" title="11. Internet Protocols and"
401 href="internet.html"><img src='../icons/up.png'
402 border='0' height='32' alt='Up One Level' width='32' /></A></td>
403<td class='online-navigation'><a rel="next" title="11.18 SimpleHTTPServer "
404 href="module-SimpleHTTPServer.html"><img src='../icons/next.png'
405 border='0' height='32' alt='Next Page' width='32' /></A></td>
406<td align="center" width="100%">Python Library Reference</td>
407<td class='online-navigation'><a rel="contents" title="Table of Contents"
408 href="contents.html"><img src='../icons/contents.png'
409 border='0' height='32' alt='Contents' width='32' /></A></td>
410<td class='online-navigation'><a href="modindex.html" title="Module Index"><img src='../icons/modules.png'
411 border='0' height='32' alt='Module Index' width='32' /></a></td>
412<td class='online-navigation'><a rel="index" title="Index"
413 href="genindex.html"><img src='../icons/index.png'
414 border='0' height='32' alt='Index' width='32' /></A></td>
415</tr></table>
416<div class='online-navigation'>
417<b class="navlabel">Previous:</b>
418<a class="sectref" rel="prev" href="node537.html">11.16.3 RequestHandler Objects</A>
419<b class="navlabel">Up:</b>
420<a class="sectref" rel="parent" href="internet.html">11. Internet Protocols and</A>
421<b class="navlabel">Next:</b>
422<a class="sectref" rel="next" href="module-SimpleHTTPServer.html">11.18 SimpleHTTPServer </A>
423</div>
424</div>
425<hr />
426<span class="release-info">Release 2.4.2, documentation updated on 28 September 2005.</span>
427</DIV>
428<!--End of Navigation Panel-->
429<ADDRESS>
430See <i><a href="about.html">About this document...</a></i> for information on suggesting changes.
431</ADDRESS>
432</BODY>
433</HTML>