1 """HTTP server base class.
3 Note: the class in this module doesn't implement any HTTP request; see
4 SimpleHTTPServer for simple implementations of GET, HEAD and POST
5 (including CGI scripts).
9 - BaseHTTPRequestHandler: HTTP request handler base class
15 - log requests even later (to capture byte count)
16 - log user-agent header and other interesting goodies
17 - send error log to separate file
18 - are request names really case sensitive?
25 # HTTP Working Group T. Berners-Lee
26 # INTERNET-DRAFT R. T. Fielding
27 # <draft-ietf-http-v10-spec-00.txt> H. Frystyk Nielsen
28 # Expires September 8, 1995 March 8, 1995
30 # URL: http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-v10-spec-00.txt
36 # Here's a quote from the NCSA httpd docs about log file format.
38 # | The logfile format is as follows. Each line consists of:
40 # | host rfc931 authuser [DD/Mon/YYYY:hh:mm:ss] "request" ddd bbbb
42 # | host: Either the DNS name or the IP number of the remote client
43 # | rfc931: Any information returned by identd for this person,
45 # | authuser: If user sent a userid for authentication, the user name,
48 # | Mon: Month (calendar name)
50 # | hh: hour (24-hour format, the machine's timezone)
53 # | request: The first line of the HTTP request as sent by the client.
54 # | ddd: the status code returned by the server, - if not available.
55 # | bbbb: the total number of bytes sent,
56 # | *not including the HTTP/1.0 header*, - if not available
58 # | You can determine the name of the file accessed through request.
60 # (Actually, the latter is only true if you know the server configuration
61 # at the time the request was made!)
69 import socket
# For gethostbyaddr()
74 # Default error message
75 DEFAULT_ERROR_MESSAGE
= """\
77 <title>Error response</title>
80 <h1>Error response</h1>
81 <p>Error code %(code)d.
82 <p>Message: %(message)s.
83 <p>Error code explanation: %(code)s = %(explain)s.
88 class HTTPServer(SocketServer
.TCPServer
):
90 def server_bind(self
):
91 """Override server_bind to store the server name."""
92 SocketServer
.TCPServer
.server_bind(self
)
93 host
, port
= self
.socket
.getsockname()
94 if not host
or host
== '0.0.0.0':
95 host
= socket
.gethostname()
97 hostname
, hostnames
, hostaddrs
= socket
.gethostbyaddr(host
)
101 if '.' not in hostname
:
102 for host
in hostnames
:
106 self
.server_name
= hostname
107 self
.server_port
= port
110 class BaseHTTPRequestHandler(SocketServer
.StreamRequestHandler
):
112 """HTTP request handler base class.
114 The following explanation of HTTP serves to guide you through the
115 code as well as to expose any misunderstandings I may have about
116 HTTP (so you don't need to read the code to figure out I'm wrong
119 HTTP (HyperText Transfer Protocol) is an extensible protocol on
120 top of a reliable stream transport (e.g. TCP/IP). The protocol
121 recognizes three parts to a request:
123 1. One line identifying the request type and path
124 2. An optional set of RFC-822-style headers
125 3. An optional data part
127 The headers and data are separated by a blank line.
129 The first line of the request has the form
131 <command> <path> <version>
133 where <command> is a (case-sensitive) keyword such as GET or POST,
134 <path> is a string containing path information for the request,
135 and <version> should be the string "HTTP/1.0". <path> is encoded
136 using the URL encoding scheme (using %xx to signify the ASCII
137 character with hex code xx).
139 The protocol is vague about whether lines are separated by LF
140 characters or by CRLF pairs -- for compatibility with the widest
141 range of clients, both should be accepted. Similarly, whitespace
142 in the request line should be treated sensibly (allowing multiple
143 spaces between components and allowing trailing whitespace).
145 Similarly, for output, lines ought to be separated by CRLF pairs
146 but most clients grok LF characters just fine.
148 If the first line of the request has the form
152 (i.e. <version> is left out) then this is assumed to be an HTTP
153 0.9 request; this form has no optional headers and data part and
154 the reply consists of just the data.
156 The reply form of the HTTP 1.0 protocol again has three parts:
158 1. One line giving the response code
159 2. An optional set of RFC-822-style headers
162 Again, the headers and data are separated by a blank line.
164 The response code line has the form
166 <version> <responsecode> <responsestring>
168 where <version> is the protocol version (always "HTTP/1.0"),
169 <responsecode> is a 3-digit response code indicating success or
170 failure of the request, and <responsestring> is an optional
171 human-readable string explaining what the response code means.
173 This server parses the request and the headers, and then calls a
174 function specific to the request type (<command>). Specifically,
175 a request SPAM will be handled by a method do_SPAM(). If no
176 such method exists the server sends an error response to the
177 client. If it exists, it is called with no arguments:
181 Note that the request name is case sensitive (i.e. SPAM and spam
182 are different requests).
184 The various request details are stored in instance variables:
186 - client_address is the client IP address in the form (host,
189 - command, path and version are the broken-down request line;
191 - headers is an instance of mimetools.Message (or a derived
192 class) containing the header information;
194 - rfile is a file object open for reading positioned at the
195 start of the optional input data part;
197 - wfile is a file object open for writing.
199 IT IS IMPORTANT TO ADHERE TO THE PROTOCOL FOR WRITING!
201 The first thing to be written must be the response line. Then
202 follow 0 or more header lines, then a blank line, and then the
203 actual data (if any). The meaning of the header lines depends on
204 the command executed by the server; in most cases, when data is
205 returned, there should be at least one header line of the form
207 Content-type: <type>/<subtype>
209 where <type> and <subtype> should be registered MIME types,
210 e.g. "text/html" or "text/plain".
214 # The Python system version, truncated to its first component.
215 sys_version
= "Python/" + string
.split(sys
.version
)[0]
217 # The server software version. You may want to override this.
218 # The format is multiple whitespace-separated strings,
219 # where each string is of the form name[/version].
220 server_version
= "BaseHTTP/" + __version__
222 def parse_request(self
):
223 """Parse a request (internal).
225 The request should be stored in self.raw_request; the results
226 are in self.command, self.path, self.request_version and
229 Return value is 1 for success, 0 for failure; on failure, an
233 self
.request_version
= version
= "HTTP/0.9" # Default
234 requestline
= self
.raw_requestline
235 if requestline
[-2:] == '\r\n':
236 requestline
= requestline
[:-2]
237 elif requestline
[-1:] == '\n':
238 requestline
= requestline
[:-1]
239 self
.requestline
= requestline
240 words
= string
.split(requestline
)
242 [command
, path
, version
] = words
243 if version
[:5] != 'HTTP/':
244 self
.send_error(400, "Bad request version (%s)" % `version`
)
246 elif len(words
) == 2:
247 [command
, path
] = words
250 "Bad HTTP/0.9 request type (%s)" % `command`
)
253 self
.send_error(400, "Bad request syntax (%s)" % `requestline`
)
255 self
.command
, self
.path
, self
.request_version
= command
, path
, version
256 self
.headers
= self
.MessageClass(self
.rfile
, 0)
260 """Handle a single HTTP request.
262 You normally don't need to override this method; see the class
263 __doc__ string for information on how to handle specific HTTP
264 commands such as GET and POST.
268 self
.raw_requestline
= self
.rfile
.readline()
269 if not self
.parse_request(): # An error code has been sent, just exit
271 mname
= 'do_' + self
.command
272 if not hasattr(self
, mname
):
273 self
.send_error(501, "Unsupported method (%s)" % `self
.command`
)
275 method
= getattr(self
, mname
)
278 def send_error(self
, code
, message
=None):
279 """Send and log an error reply.
281 Arguments are the error code, and a detailed message.
282 The detailed message defaults to the short entry matching the
285 This sends an error response (so it must be called before any
286 output has been generated), logs the error, and finally sends
287 a piece of HTML explaining the error to the user.
292 short
, long = self
.responses
[code
]
294 short
, long = '???', '???'
298 self
.log_error("code %d, message %s", code
, message
)
299 self
.send_response(code
, message
)
301 self
.wfile
.write(self
.error_message_format
%
306 error_message_format
= DEFAULT_ERROR_MESSAGE
308 def send_response(self
, code
, message
=None):
309 """Send the response header and log the response code.
311 Also send two standard headers with the server software
312 version and the current date.
315 self
.log_request(code
)
317 if self
.responses
.has_key(code
):
318 message
= self
.responses
[code
][0]
321 if self
.request_version
!= 'HTTP/0.9':
322 self
.wfile
.write("%s %s %s\r\n" %
323 (self
.protocol_version
, str(code
), message
))
324 self
.send_header('Server', self
.version_string())
325 self
.send_header('Date', self
.date_time_string())
327 def send_header(self
, keyword
, value
):
328 """Send a MIME header."""
329 if self
.request_version
!= 'HTTP/0.9':
330 self
.wfile
.write("%s: %s\r\n" % (keyword
, value
))
332 def end_headers(self
):
333 """Send the blank line ending the MIME headers."""
334 if self
.request_version
!= 'HTTP/0.9':
335 self
.wfile
.write("\r\n")
337 def log_request(self
, code
='-', size
='-'):
338 """Log an accepted request.
340 This is called by send_reponse().
344 self
.log_message('"%s" %s %s',
345 self
.requestline
, str(code
), str(size
))
347 def log_error(self
, *args
):
350 This is called when a request cannot be fulfilled. By
351 default it passes the message on to log_message().
353 Arguments are the same as for log_message().
355 XXX This should go to the separate error log.
359 apply(self
.log_message
, args
)
361 def log_message(self
, format
, *args
):
362 """Log an arbitrary message.
364 This is used by all other logging functions. Override
365 it if you have specific logging wishes.
367 The first argument, FORMAT, is a format string for the
368 message to be logged. If the format string contains
369 any % escapes requiring parameters, they should be
370 specified as subsequent arguments (it's just like
373 The client host and current date/time are prefixed to
378 sys
.stderr
.write("%s - - [%s] %s\n" %
379 (self
.address_string(),
380 self
.log_date_time_string(),
383 def version_string(self
):
384 """Return the server software version string."""
385 return self
.server_version
+ ' ' + self
.sys_version
387 def date_time_string(self
):
388 """Return the current date and time formatted for a message header."""
390 year
, month
, day
, hh
, mm
, ss
, wd
, y
, z
= time
.gmtime(now
)
391 s
= "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
392 self
.weekdayname
[wd
],
393 day
, self
.monthname
[month
], year
,
397 def log_date_time_string(self
):
398 """Return the current time formatted for logging."""
400 year
, month
, day
, hh
, mm
, ss
, x
, y
, z
= time
.localtime(now
)
401 s
= "%02d/%3s/%04d %02d:%02d:%02d" % (
402 day
, self
.monthname
[month
], year
, hh
, mm
, ss
)
405 weekdayname
= ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
408 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
409 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
411 def address_string(self
):
412 """Return the client address formatted for logging.
414 This version looks up the full hostname using gethostbyaddr(),
415 and tries to find a name that contains at least one dot.
419 (host
, port
) = self
.client_address
421 name
, names
, addresses
= socket
.gethostbyaddr(host
)
422 except socket
.error
, msg
:
424 names
.insert(0, name
)
426 if '.' in name
: return name
430 # Essentially static class variables
432 # The version of the HTTP protocol we support.
433 # Don't override unless you know what you're doing (hint: incoming
434 # requests are required to have exactly this version string).
435 protocol_version
= "HTTP/1.0"
437 # The Message-like class used to parse headers
438 MessageClass
= mimetools
.Message
440 # Table mapping response codes to messages; entries have the
441 # form {code: (shortmessage, longmessage)}.
442 # See http://www.w3.org/hypertext/WWW/Protocols/HTTP/HTRESP.html
444 200: ('OK', 'Request fulfilled, document follows'),
445 201: ('Created', 'Document created, URL follows'),
447 'Request accepted, processing continues off-line'),
448 203: ('Partial information', 'Request fulfilled from cache'),
449 204: ('No response', 'Request fulfilled, nothing follows'),
451 301: ('Moved', 'Object moved permanently -- see URI list'),
452 302: ('Found', 'Object moved temporarily -- see URI list'),
453 303: ('Method', 'Object moved -- see Method and URL list'),
454 304: ('Not modified',
455 'Document has not changed singe given time'),
458 'Bad request syntax or unsupported method'),
459 401: ('Unauthorized',
460 'No permission -- see authorization schemes'),
461 402: ('Payment required',
462 'No payment -- see charging schemes'),
464 'Request forbidden -- authorization will not help'),
465 404: ('Not found', 'Nothing matches the given URI'),
467 500: ('Internal error', 'Server got itself in trouble'),
468 501: ('Not implemented',
469 'Server does not support this operation'),
470 502: ('Service temporarily overloaded',
471 'The server cannot process the request due to a high load'),
472 503: ('Gateway timeout',
473 'The gateway server did not receive a timely response'),
478 def test(HandlerClass
= BaseHTTPRequestHandler
,
479 ServerClass
= HTTPServer
):
480 """Test the HTTP request handler class.
482 This runs an HTTP server on port 8000 (or the first command line
488 port
= string
.atoi(sys
.argv
[1])
491 server_address
= ('', port
)
493 httpd
= ServerClass(server_address
, HandlerClass
)
495 print "Serving HTTP on port", port
, "..."
496 httpd
.serve_forever()
499 if __name__
== '__main__':