blob: b527163bf041a5a20c6a1fcbdb98f1f157af39f0 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`BaseHTTPServer` --- Basic HTTP server
3===========================================
4
5.. module:: BaseHTTPServer
6 :synopsis: Basic HTTP server (base class for SimpleHTTPServer and CGIHTTPServer).
7
8
9.. index::
10 pair: WWW; server
11 pair: HTTP; protocol
12 single: URL
13 single: httpd
14
15.. index::
16 module: SimpleHTTPServer
17 module: CGIHTTPServer
18
19This module defines two classes for implementing HTTP servers (Web servers).
20Usually, this module isn't used directly, but is used as a basis for building
21functioning Web servers. See the :mod:`SimpleHTTPServer` and
22:mod:`CGIHTTPServer` modules.
23
24The first class, :class:`HTTPServer`, is a :class:`SocketServer.TCPServer`
25subclass. It creates and listens at the HTTP socket, dispatching the requests
26to a handler. Code to create and run the server looks like this::
27
28 def run(server_class=BaseHTTPServer.HTTPServer,
29 handler_class=BaseHTTPServer.BaseHTTPRequestHandler):
30 server_address = ('', 8000)
31 httpd = server_class(server_address, handler_class)
32 httpd.serve_forever()
33
34
35.. class:: HTTPServer(server_address, RequestHandlerClass)
36
37 This class builds on the :class:`TCPServer` class by storing the server address
38 as instance variables named :attr:`server_name` and :attr:`server_port`. The
39 server is accessible by the handler, typically through the handler's
40 :attr:`server` instance variable.
41
42
43.. class:: BaseHTTPRequestHandler(request, client_address, server)
44
45 This class is used to handle the HTTP requests that arrive at the server. By
46 itself, it cannot respond to any actual HTTP requests; it must be subclassed to
47 handle each request method (e.g. GET or POST). :class:`BaseHTTPRequestHandler`
48 provides a number of class and instance variables, and methods for use by
49 subclasses.
50
51 The handler will parse the request and the headers, then call a method specific
52 to the request type. The method name is constructed from the request. For
53 example, for the request method ``SPAM``, the :meth:`do_SPAM` method will be
54 called with no arguments. All of the relevant information is stored in instance
55 variables of the handler. Subclasses should not need to override or extend the
56 :meth:`__init__` method.
57
58:class:`BaseHTTPRequestHandler` has the following instance variables:
59
60
61.. attribute:: BaseHTTPRequestHandler.client_address
62
63 Contains a tuple of the form ``(host, port)`` referring to the client's address.
64
65
66.. attribute:: BaseHTTPRequestHandler.command
67
68 Contains the command (request type). For example, ``'GET'``.
69
70
71.. attribute:: BaseHTTPRequestHandler.path
72
73 Contains the request path.
74
75
76.. attribute:: BaseHTTPRequestHandler.request_version
77
78 Contains the version string from the request. For example, ``'HTTP/1.0'``.
79
80
81.. attribute:: BaseHTTPRequestHandler.headers
82
83 Holds an instance of the class specified by the :attr:`MessageClass` class
84 variable. This instance parses and manages the headers in the HTTP request.
85
86
87.. attribute:: BaseHTTPRequestHandler.rfile
88
89 Contains an input stream, positioned at the start of the optional input data.
90
91
92.. attribute:: BaseHTTPRequestHandler.wfile
93
94 Contains the output stream for writing a response back to the client. Proper
95 adherence to the HTTP protocol must be used when writing to this stream.
96
97:class:`BaseHTTPRequestHandler` has the following class variables:
98
99
100.. attribute:: BaseHTTPRequestHandler.server_version
101
102 Specifies the server software version. You may want to override this. The
103 format is multiple whitespace-separated strings, where each string is of the
104 form name[/version]. For example, ``'BaseHTTP/0.2'``.
105
106
107.. attribute:: BaseHTTPRequestHandler.sys_version
108
109 Contains the Python system version, in a form usable by the
110 :attr:`version_string` method and the :attr:`server_version` class variable. For
111 example, ``'Python/1.4'``.
112
113
114.. attribute:: BaseHTTPRequestHandler.error_message_format
115
116 Specifies a format string for building an error response to the client. It uses
117 parenthesized, keyed format specifiers, so the format operand must be a
118 dictionary. The *code* key should be an integer, specifying the numeric HTTP
119 error code value. *message* should be a string containing a (detailed) error
120 message of what occurred, and *explain* should be an explanation of the error
121 code number. Default *message* and *explain* values can found in the *responses*
122 class variable.
123
124
Christian Heimes8640e742008-02-23 16:23:06 +0000125.. attribute:: BaseHTTPRequestHandler.error_content_type
126
127 Specifies the Content-Type HTTP header of error responses sent to the client.
128 The default value is ``'text/html'``.
129
Christian Heimes8640e742008-02-23 16:23:06 +0000130
Georg Brandl116aa622007-08-15 14:28:22 +0000131.. attribute:: BaseHTTPRequestHandler.protocol_version
132
133 This specifies the HTTP protocol version used in responses. If set to
134 ``'HTTP/1.1'``, the server will permit HTTP persistent connections; however,
135 your server *must* then include an accurate ``Content-Length`` header (using
136 :meth:`send_header`) in all of its responses to clients. For backwards
137 compatibility, the setting defaults to ``'HTTP/1.0'``.
138
139
140.. attribute:: BaseHTTPRequestHandler.MessageClass
141
142 .. index:: single: Message (in module mimetools)
143
144 Specifies a :class:`rfc822.Message`\ -like class to parse HTTP headers.
145 Typically, this is not overridden, and it defaults to
146 :class:`mimetools.Message`.
147
148
149.. attribute:: BaseHTTPRequestHandler.responses
150
151 This variable contains a mapping of error code integers to two-element tuples
152 containing a short and long message. For example, ``{code: (shortmessage,
153 longmessage)}``. The *shortmessage* is usually used as the *message* key in an
154 error response, and *longmessage* as the *explain* key (see the
155 :attr:`error_message_format` class variable).
156
157A :class:`BaseHTTPRequestHandler` instance has the following methods:
158
159
160.. method:: BaseHTTPRequestHandler.handle()
161
162 Calls :meth:`handle_one_request` once (or, if persistent connections are
163 enabled, multiple times) to handle incoming HTTP requests. You should never need
164 to override it; instead, implement appropriate :meth:`do_\*` methods.
165
166
167.. method:: BaseHTTPRequestHandler.handle_one_request()
168
169 This method will parse and dispatch the request to the appropriate :meth:`do_\*`
170 method. You should never need to override it.
171
172
173.. method:: BaseHTTPRequestHandler.send_error(code[, message])
174
175 Sends and logs a complete error reply to the client. The numeric *code*
176 specifies the HTTP error code, with *message* as optional, more specific text. A
177 complete set of headers is sent, followed by text composed using the
178 :attr:`error_message_format` class variable.
179
180
181.. method:: BaseHTTPRequestHandler.send_response(code[, message])
182
183 Sends a response header and logs the accepted request. The HTTP response line is
184 sent, followed by *Server* and *Date* headers. The values for these two headers
185 are picked up from the :meth:`version_string` and :meth:`date_time_string`
186 methods, respectively.
187
188
189.. method:: BaseHTTPRequestHandler.send_header(keyword, value)
190
191 Writes a specific HTTP header to the output stream. *keyword* should specify the
192 header keyword, with *value* specifying its value.
193
194
195.. method:: BaseHTTPRequestHandler.end_headers()
196
197 Sends a blank line, indicating the end of the HTTP headers in the response.
198
199
200.. method:: BaseHTTPRequestHandler.log_request([code[, size]])
201
202 Logs an accepted (successful) request. *code* should specify the numeric HTTP
203 code associated with the response. If a size of the response is available, then
204 it should be passed as the *size* parameter.
205
206
207.. method:: BaseHTTPRequestHandler.log_error(...)
208
209 Logs an error when a request cannot be fulfilled. By default, it passes the
210 message to :meth:`log_message`, so it takes the same arguments (*format* and
211 additional values).
212
213
214.. method:: BaseHTTPRequestHandler.log_message(format, ...)
215
216 Logs an arbitrary message to ``sys.stderr``. This is typically overridden to
217 create custom error logging mechanisms. The *format* argument is a standard
218 printf-style format string, where the additional arguments to
219 :meth:`log_message` are applied as inputs to the formatting. The client address
220 and current date and time are prefixed to every message logged.
221
222
223.. method:: BaseHTTPRequestHandler.version_string()
224
225 Returns the server software's version string. This is a combination of the
226 :attr:`server_version` and :attr:`sys_version` class variables.
227
228
229.. method:: BaseHTTPRequestHandler.date_time_string([timestamp])
230
231 Returns the date and time given by *timestamp* (which must be in the format
232 returned by :func:`time.time`), formatted for a message header. If *timestamp*
233 is omitted, it uses the current date and time.
234
235 The result looks like ``'Sun, 06 Nov 1994 08:49:37 GMT'``.
236
Georg Brandl116aa622007-08-15 14:28:22 +0000237
238.. method:: BaseHTTPRequestHandler.log_date_time_string()
239
240 Returns the current date and time, formatted for logging.
241
242
243.. method:: BaseHTTPRequestHandler.address_string()
244
245 Returns the client address, formatted for logging. A name lookup is performed on
246 the client's IP address.
247
248
249.. seealso::
250
251 Module :mod:`CGIHTTPServer`
252 Extended request handler that supports CGI scripts.
253
254 Module :mod:`SimpleHTTPServer`
255 Basic request handler that limits response to files actually under the document
256 root.
257