blob: 0f058a18e1657e4920633c77246f8bcf671cbeb8 [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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
Georg Brandl16479232008-02-23 15:02:28 +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
130 .. versionadded:: 2.6
131 Previously, the content type was always ``'text/html'``.
132
133
Georg Brandl8ec7f652007-08-15 14:28:01 +0000134.. attribute:: BaseHTTPRequestHandler.protocol_version
135
136 This specifies the HTTP protocol version used in responses. If set to
137 ``'HTTP/1.1'``, the server will permit HTTP persistent connections; however,
138 your server *must* then include an accurate ``Content-Length`` header (using
139 :meth:`send_header`) in all of its responses to clients. For backwards
140 compatibility, the setting defaults to ``'HTTP/1.0'``.
141
142
143.. attribute:: BaseHTTPRequestHandler.MessageClass
144
145 .. index:: single: Message (in module mimetools)
146
147 Specifies a :class:`rfc822.Message`\ -like class to parse HTTP headers.
148 Typically, this is not overridden, and it defaults to
149 :class:`mimetools.Message`.
150
151
152.. attribute:: BaseHTTPRequestHandler.responses
153
154 This variable contains a mapping of error code integers to two-element tuples
155 containing a short and long message. For example, ``{code: (shortmessage,
156 longmessage)}``. The *shortmessage* is usually used as the *message* key in an
157 error response, and *longmessage* as the *explain* key (see the
158 :attr:`error_message_format` class variable).
159
160A :class:`BaseHTTPRequestHandler` instance has the following methods:
161
162
163.. method:: BaseHTTPRequestHandler.handle()
164
165 Calls :meth:`handle_one_request` once (or, if persistent connections are
166 enabled, multiple times) to handle incoming HTTP requests. You should never need
167 to override it; instead, implement appropriate :meth:`do_\*` methods.
168
169
170.. method:: BaseHTTPRequestHandler.handle_one_request()
171
172 This method will parse and dispatch the request to the appropriate :meth:`do_\*`
173 method. You should never need to override it.
174
175
176.. method:: BaseHTTPRequestHandler.send_error(code[, message])
177
178 Sends and logs a complete error reply to the client. The numeric *code*
179 specifies the HTTP error code, with *message* as optional, more specific text. A
180 complete set of headers is sent, followed by text composed using the
181 :attr:`error_message_format` class variable.
182
183
184.. method:: BaseHTTPRequestHandler.send_response(code[, message])
185
186 Sends a response header and logs the accepted request. The HTTP response line is
187 sent, followed by *Server* and *Date* headers. The values for these two headers
188 are picked up from the :meth:`version_string` and :meth:`date_time_string`
189 methods, respectively.
190
191
192.. method:: BaseHTTPRequestHandler.send_header(keyword, value)
193
194 Writes a specific HTTP header to the output stream. *keyword* should specify the
195 header keyword, with *value* specifying its value.
196
197
198.. method:: BaseHTTPRequestHandler.end_headers()
199
200 Sends a blank line, indicating the end of the HTTP headers in the response.
201
202
203.. method:: BaseHTTPRequestHandler.log_request([code[, size]])
204
205 Logs an accepted (successful) request. *code* should specify the numeric HTTP
206 code associated with the response. If a size of the response is available, then
207 it should be passed as the *size* parameter.
208
209
210.. method:: BaseHTTPRequestHandler.log_error(...)
211
212 Logs an error when a request cannot be fulfilled. By default, it passes the
213 message to :meth:`log_message`, so it takes the same arguments (*format* and
214 additional values).
215
216
217.. method:: BaseHTTPRequestHandler.log_message(format, ...)
218
219 Logs an arbitrary message to ``sys.stderr``. This is typically overridden to
220 create custom error logging mechanisms. The *format* argument is a standard
221 printf-style format string, where the additional arguments to
222 :meth:`log_message` are applied as inputs to the formatting. The client address
223 and current date and time are prefixed to every message logged.
224
225
226.. method:: BaseHTTPRequestHandler.version_string()
227
228 Returns the server software's version string. This is a combination of the
229 :attr:`server_version` and :attr:`sys_version` class variables.
230
231
232.. method:: BaseHTTPRequestHandler.date_time_string([timestamp])
233
234 Returns the date and time given by *timestamp* (which must be in the format
235 returned by :func:`time.time`), formatted for a message header. If *timestamp*
236 is omitted, it uses the current date and time.
237
238 The result looks like ``'Sun, 06 Nov 1994 08:49:37 GMT'``.
239
240 .. versionadded:: 2.5
241 The *timestamp* parameter.
242
243
244.. method:: BaseHTTPRequestHandler.log_date_time_string()
245
246 Returns the current date and time, formatted for logging.
247
248
249.. method:: BaseHTTPRequestHandler.address_string()
250
251 Returns the client address, formatted for logging. A name lookup is performed on
252 the client's IP address.
253
254
255.. seealso::
256
257 Module :mod:`CGIHTTPServer`
258 Extended request handler that supports CGI scripts.
259
260 Module :mod:`SimpleHTTPServer`
261 Basic request handler that limits response to files actually under the document
262 root.
263