Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 1 | """HTTP server base class. |
| 2 | |
| 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). |
| 6 | |
| 7 | Contents: |
| 8 | |
| 9 | - BaseHTTPRequestHandler: HTTP request handler base class |
| 10 | - test: test function |
| 11 | |
| 12 | XXX To do: |
| 13 | |
| 14 | - send server version |
| 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? |
| 19 | |
| 20 | """ |
| 21 | |
| 22 | |
| 23 | # See also: |
| 24 | # |
| 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 |
| 29 | # |
| 30 | # URL: http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-v10-spec-00.txt |
| 31 | |
| 32 | |
| 33 | # Log files |
| 34 | # --------- |
| 35 | # |
| 36 | # Here's a quote from the NCSA httpd docs about log file format. |
| 37 | # |
| 38 | # | The logfile format is as follows. Each line consists of: |
| 39 | # | |
| 40 | # | host rfc931 authuser [DD/Mon/YYYY:hh:mm:ss] "request" ddd bbbb |
| 41 | # | |
| 42 | # | host: Either the DNS name or the IP number of the remote client |
| 43 | # | rfc931: Any information returned by identd for this person, |
| 44 | # | - otherwise. |
| 45 | # | authuser: If user sent a userid for authentication, the user name, |
| 46 | # | - otherwise. |
| 47 | # | DD: Day |
| 48 | # | Mon: Month (calendar name) |
| 49 | # | YYYY: Year |
| 50 | # | hh: hour (24-hour format, the machine's timezone) |
| 51 | # | mm: minutes |
| 52 | # | ss: seconds |
| 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 |
| 57 | # | |
| 58 | # | You can determine the name of the file accessed through request. |
| 59 | # |
| 60 | # (Actually, the latter is only true if you know the server configuration |
| 61 | # at the time the request was made!) |
| 62 | |
| 63 | |
| 64 | __version__ = "0.2" |
| 65 | |
| 66 | |
| 67 | import sys |
| 68 | import time |
| 69 | import socket # For gethostbyaddr() |
| 70 | import string |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 71 | import mimetools |
| 72 | import SocketServer |
| 73 | |
| 74 | # Default error message |
| 75 | DEFAULT_ERROR_MESSAGE = """\ |
| 76 | <head> |
| 77 | <title>Error response</title> |
| 78 | </head> |
| 79 | <body> |
| 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. |
| 84 | </body> |
| 85 | """ |
| 86 | |
| 87 | |
| 88 | class HTTPServer(SocketServer.TCPServer): |
| 89 | |
Guido van Rossum | 3e06ab1 | 2000-06-29 19:35:29 +0000 | [diff] [blame^] | 90 | allow_reuse_address = 1 # Seems to make sense in testing environment |
| 91 | |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 92 | def server_bind(self): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 93 | """Override server_bind to store the server name.""" |
| 94 | SocketServer.TCPServer.server_bind(self) |
| 95 | host, port = self.socket.getsockname() |
| 96 | if not host or host == '0.0.0.0': |
| 97 | host = socket.gethostname() |
Guido van Rossum | aad6761 | 2000-05-08 17:31:04 +0000 | [diff] [blame] | 98 | try: |
| 99 | hostname, hostnames, hostaddrs = socket.gethostbyaddr(host) |
| 100 | except socket.error: |
| 101 | hostname = host |
| 102 | else: |
| 103 | if '.' not in hostname: |
| 104 | for host in hostnames: |
| 105 | if '.' in host: |
| 106 | hostname = host |
| 107 | break |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 108 | self.server_name = hostname |
| 109 | self.server_port = port |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 110 | |
| 111 | |
| 112 | class BaseHTTPRequestHandler(SocketServer.StreamRequestHandler): |
| 113 | |
| 114 | """HTTP request handler base class. |
| 115 | |
| 116 | The following explanation of HTTP serves to guide you through the |
| 117 | code as well as to expose any misunderstandings I may have about |
| 118 | HTTP (so you don't need to read the code to figure out I'm wrong |
| 119 | :-). |
| 120 | |
| 121 | HTTP (HyperText Transfer Protocol) is an extensible protocol on |
| 122 | top of a reliable stream transport (e.g. TCP/IP). The protocol |
| 123 | recognizes three parts to a request: |
| 124 | |
| 125 | 1. One line identifying the request type and path |
| 126 | 2. An optional set of RFC-822-style headers |
| 127 | 3. An optional data part |
| 128 | |
| 129 | The headers and data are separated by a blank line. |
| 130 | |
| 131 | The first line of the request has the form |
| 132 | |
| 133 | <command> <path> <version> |
| 134 | |
| 135 | where <command> is a (case-sensitive) keyword such as GET or POST, |
| 136 | <path> is a string containing path information for the request, |
| 137 | and <version> should be the string "HTTP/1.0". <path> is encoded |
| 138 | using the URL encoding scheme (using %xx to signify the ASCII |
| 139 | character with hex code xx). |
| 140 | |
| 141 | The protocol is vague about whether lines are separated by LF |
| 142 | characters or by CRLF pairs -- for compatibility with the widest |
| 143 | range of clients, both should be accepted. Similarly, whitespace |
| 144 | in the request line should be treated sensibly (allowing multiple |
| 145 | spaces between components and allowing trailing whitespace). |
| 146 | |
| 147 | Similarly, for output, lines ought to be separated by CRLF pairs |
| 148 | but most clients grok LF characters just fine. |
| 149 | |
| 150 | If the first line of the request has the form |
| 151 | |
| 152 | <command> <path> |
| 153 | |
| 154 | (i.e. <version> is left out) then this is assumed to be an HTTP |
| 155 | 0.9 request; this form has no optional headers and data part and |
| 156 | the reply consists of just the data. |
| 157 | |
| 158 | The reply form of the HTTP 1.0 protocol again has three parts: |
| 159 | |
| 160 | 1. One line giving the response code |
| 161 | 2. An optional set of RFC-822-style headers |
| 162 | 3. The data |
| 163 | |
| 164 | Again, the headers and data are separated by a blank line. |
| 165 | |
| 166 | The response code line has the form |
| 167 | |
| 168 | <version> <responsecode> <responsestring> |
| 169 | |
| 170 | where <version> is the protocol version (always "HTTP/1.0"), |
| 171 | <responsecode> is a 3-digit response code indicating success or |
| 172 | failure of the request, and <responsestring> is an optional |
| 173 | human-readable string explaining what the response code means. |
| 174 | |
| 175 | This server parses the request and the headers, and then calls a |
| 176 | function specific to the request type (<command>). Specifically, |
Guido van Rossum | aad6761 | 2000-05-08 17:31:04 +0000 | [diff] [blame] | 177 | a request SPAM will be handled by a method do_SPAM(). If no |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 178 | such method exists the server sends an error response to the |
| 179 | client. If it exists, it is called with no arguments: |
| 180 | |
| 181 | do_SPAM() |
| 182 | |
| 183 | Note that the request name is case sensitive (i.e. SPAM and spam |
| 184 | are different requests). |
| 185 | |
| 186 | The various request details are stored in instance variables: |
| 187 | |
| 188 | - client_address is the client IP address in the form (host, |
| 189 | port); |
| 190 | |
| 191 | - command, path and version are the broken-down request line; |
| 192 | |
| 193 | - headers is an instance of mimetools.Message (or a derived |
| 194 | class) containing the header information; |
| 195 | |
| 196 | - rfile is a file object open for reading positioned at the |
| 197 | start of the optional input data part; |
| 198 | |
| 199 | - wfile is a file object open for writing. |
| 200 | |
| 201 | IT IS IMPORTANT TO ADHERE TO THE PROTOCOL FOR WRITING! |
| 202 | |
| 203 | The first thing to be written must be the response line. Then |
| 204 | follow 0 or more header lines, then a blank line, and then the |
| 205 | actual data (if any). The meaning of the header lines depends on |
| 206 | the command executed by the server; in most cases, when data is |
| 207 | returned, there should be at least one header line of the form |
| 208 | |
| 209 | Content-type: <type>/<subtype> |
| 210 | |
| 211 | where <type> and <subtype> should be registered MIME types, |
| 212 | e.g. "text/html" or "text/plain". |
| 213 | |
| 214 | """ |
| 215 | |
| 216 | # The Python system version, truncated to its first component. |
| 217 | sys_version = "Python/" + string.split(sys.version)[0] |
| 218 | |
| 219 | # The server software version. You may want to override this. |
| 220 | # The format is multiple whitespace-separated strings, |
| 221 | # where each string is of the form name[/version]. |
| 222 | server_version = "BaseHTTP/" + __version__ |
| 223 | |
Guido van Rossum | aad6761 | 2000-05-08 17:31:04 +0000 | [diff] [blame] | 224 | def parse_request(self): |
| 225 | """Parse a request (internal). |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 226 | |
Guido van Rossum | aad6761 | 2000-05-08 17:31:04 +0000 | [diff] [blame] | 227 | The request should be stored in self.raw_request; the results |
| 228 | are in self.command, self.path, self.request_version and |
| 229 | self.headers. |
| 230 | |
| 231 | Return value is 1 for success, 0 for failure; on failure, an |
| 232 | error is sent back. |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 233 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 234 | """ |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 235 | self.request_version = version = "HTTP/0.9" # Default |
| 236 | requestline = self.raw_requestline |
| 237 | if requestline[-2:] == '\r\n': |
| 238 | requestline = requestline[:-2] |
| 239 | elif requestline[-1:] == '\n': |
| 240 | requestline = requestline[:-1] |
| 241 | self.requestline = requestline |
| 242 | words = string.split(requestline) |
| 243 | if len(words) == 3: |
| 244 | [command, path, version] = words |
| 245 | if version[:5] != 'HTTP/': |
| 246 | self.send_error(400, "Bad request version (%s)" % `version`) |
Guido van Rossum | aad6761 | 2000-05-08 17:31:04 +0000 | [diff] [blame] | 247 | return 0 |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 248 | elif len(words) == 2: |
| 249 | [command, path] = words |
| 250 | if command != 'GET': |
| 251 | self.send_error(400, |
| 252 | "Bad HTTP/0.9 request type (%s)" % `command`) |
Guido van Rossum | aad6761 | 2000-05-08 17:31:04 +0000 | [diff] [blame] | 253 | return 0 |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 254 | else: |
| 255 | self.send_error(400, "Bad request syntax (%s)" % `requestline`) |
Guido van Rossum | aad6761 | 2000-05-08 17:31:04 +0000 | [diff] [blame] | 256 | return 0 |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 257 | self.command, self.path, self.request_version = command, path, version |
| 258 | self.headers = self.MessageClass(self.rfile, 0) |
Guido van Rossum | aad6761 | 2000-05-08 17:31:04 +0000 | [diff] [blame] | 259 | return 1 |
| 260 | |
| 261 | def handle(self): |
| 262 | """Handle a single HTTP request. |
| 263 | |
| 264 | You normally don't need to override this method; see the class |
| 265 | __doc__ string for information on how to handle specific HTTP |
| 266 | commands such as GET and POST. |
| 267 | |
| 268 | """ |
| 269 | |
| 270 | self.raw_requestline = self.rfile.readline() |
| 271 | if not self.parse_request(): # An error code has been sent, just exit |
| 272 | return |
| 273 | mname = 'do_' + self.command |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 274 | if not hasattr(self, mname): |
Guido van Rossum | aad6761 | 2000-05-08 17:31:04 +0000 | [diff] [blame] | 275 | self.send_error(501, "Unsupported method (%s)" % `self.command`) |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 276 | return |
| 277 | method = getattr(self, mname) |
| 278 | method() |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 279 | |
| 280 | def send_error(self, code, message=None): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 281 | """Send and log an error reply. |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 282 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 283 | Arguments are the error code, and a detailed message. |
| 284 | The detailed message defaults to the short entry matching the |
| 285 | response code. |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 286 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 287 | This sends an error response (so it must be called before any |
| 288 | output has been generated), logs the error, and finally sends |
| 289 | a piece of HTML explaining the error to the user. |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 290 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 291 | """ |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 292 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 293 | try: |
| 294 | short, long = self.responses[code] |
| 295 | except KeyError: |
| 296 | short, long = '???', '???' |
| 297 | if not message: |
| 298 | message = short |
| 299 | explain = long |
| 300 | self.log_error("code %d, message %s", code, message) |
| 301 | self.send_response(code, message) |
| 302 | self.end_headers() |
| 303 | self.wfile.write(self.error_message_format % |
| 304 | {'code': code, |
| 305 | 'message': message, |
| 306 | 'explain': explain}) |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 307 | |
| 308 | error_message_format = DEFAULT_ERROR_MESSAGE |
| 309 | |
| 310 | def send_response(self, code, message=None): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 311 | """Send the response header and log the response code. |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 312 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 313 | Also send two standard headers with the server software |
| 314 | version and the current date. |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 315 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 316 | """ |
| 317 | self.log_request(code) |
| 318 | if message is None: |
| 319 | if self.responses.has_key(code): |
| 320 | message = self.responses[code][0] |
| 321 | else: |
| 322 | message = '' |
| 323 | if self.request_version != 'HTTP/0.9': |
| 324 | self.wfile.write("%s %s %s\r\n" % |
| 325 | (self.protocol_version, str(code), message)) |
| 326 | self.send_header('Server', self.version_string()) |
| 327 | self.send_header('Date', self.date_time_string()) |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 328 | |
| 329 | def send_header(self, keyword, value): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 330 | """Send a MIME header.""" |
| 331 | if self.request_version != 'HTTP/0.9': |
| 332 | self.wfile.write("%s: %s\r\n" % (keyword, value)) |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 333 | |
| 334 | def end_headers(self): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 335 | """Send the blank line ending the MIME headers.""" |
| 336 | if self.request_version != 'HTTP/0.9': |
| 337 | self.wfile.write("\r\n") |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 338 | |
| 339 | def log_request(self, code='-', size='-'): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 340 | """Log an accepted request. |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 341 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 342 | This is called by send_reponse(). |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 343 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 344 | """ |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 345 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 346 | self.log_message('"%s" %s %s', |
| 347 | self.requestline, str(code), str(size)) |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 348 | |
| 349 | def log_error(self, *args): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 350 | """Log an error. |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 351 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 352 | This is called when a request cannot be fulfilled. By |
| 353 | default it passes the message on to log_message(). |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 354 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 355 | Arguments are the same as for log_message(). |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 356 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 357 | XXX This should go to the separate error log. |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 358 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 359 | """ |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 360 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 361 | apply(self.log_message, args) |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 362 | |
| 363 | def log_message(self, format, *args): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 364 | """Log an arbitrary message. |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 365 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 366 | This is used by all other logging functions. Override |
| 367 | it if you have specific logging wishes. |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 368 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 369 | The first argument, FORMAT, is a format string for the |
| 370 | message to be logged. If the format string contains |
| 371 | any % escapes requiring parameters, they should be |
| 372 | specified as subsequent arguments (it's just like |
| 373 | printf!). |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 374 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 375 | The client host and current date/time are prefixed to |
| 376 | every message. |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 377 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 378 | """ |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 379 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 380 | sys.stderr.write("%s - - [%s] %s\n" % |
| 381 | (self.address_string(), |
| 382 | self.log_date_time_string(), |
| 383 | format%args)) |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 384 | |
| 385 | def version_string(self): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 386 | """Return the server software version string.""" |
| 387 | return self.server_version + ' ' + self.sys_version |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 388 | |
| 389 | def date_time_string(self): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 390 | """Return the current date and time formatted for a message header.""" |
| 391 | now = time.time() |
| 392 | year, month, day, hh, mm, ss, wd, y, z = time.gmtime(now) |
| 393 | s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % ( |
| 394 | self.weekdayname[wd], |
| 395 | day, self.monthname[month], year, |
| 396 | hh, mm, ss) |
| 397 | return s |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 398 | |
| 399 | def log_date_time_string(self): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 400 | """Return the current time formatted for logging.""" |
| 401 | now = time.time() |
| 402 | year, month, day, hh, mm, ss, x, y, z = time.localtime(now) |
| 403 | s = "%02d/%3s/%04d %02d:%02d:%02d" % ( |
| 404 | day, self.monthname[month], year, hh, mm, ss) |
| 405 | return s |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 406 | |
| 407 | weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] |
| 408 | |
| 409 | monthname = [None, |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 410 | 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', |
| 411 | 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 412 | |
| 413 | def address_string(self): |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 414 | """Return the client address formatted for logging. |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 415 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 416 | This version looks up the full hostname using gethostbyaddr(), |
| 417 | and tries to find a name that contains at least one dot. |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 418 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 419 | """ |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 420 | |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 421 | (host, port) = self.client_address |
| 422 | try: |
| 423 | name, names, addresses = socket.gethostbyaddr(host) |
| 424 | except socket.error, msg: |
| 425 | return host |
| 426 | names.insert(0, name) |
| 427 | for name in names: |
| 428 | if '.' in name: return name |
| 429 | return names[0] |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 430 | |
| 431 | |
| 432 | # Essentially static class variables |
| 433 | |
| 434 | # The version of the HTTP protocol we support. |
| 435 | # Don't override unless you know what you're doing (hint: incoming |
| 436 | # requests are required to have exactly this version string). |
| 437 | protocol_version = "HTTP/1.0" |
| 438 | |
| 439 | # The Message-like class used to parse headers |
| 440 | MessageClass = mimetools.Message |
| 441 | |
| 442 | # Table mapping response codes to messages; entries have the |
| 443 | # form {code: (shortmessage, longmessage)}. |
| 444 | # See http://www.w3.org/hypertext/WWW/Protocols/HTTP/HTRESP.html |
| 445 | responses = { |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 446 | 200: ('OK', 'Request fulfilled, document follows'), |
| 447 | 201: ('Created', 'Document created, URL follows'), |
| 448 | 202: ('Accepted', |
| 449 | 'Request accepted, processing continues off-line'), |
| 450 | 203: ('Partial information', 'Request fulfilled from cache'), |
| 451 | 204: ('No response', 'Request fulfilled, nothing follows'), |
| 452 | |
| 453 | 301: ('Moved', 'Object moved permanently -- see URI list'), |
| 454 | 302: ('Found', 'Object moved temporarily -- see URI list'), |
| 455 | 303: ('Method', 'Object moved -- see Method and URL list'), |
| 456 | 304: ('Not modified', |
| 457 | 'Document has not changed singe given time'), |
| 458 | |
| 459 | 400: ('Bad request', |
| 460 | 'Bad request syntax or unsupported method'), |
| 461 | 401: ('Unauthorized', |
| 462 | 'No permission -- see authorization schemes'), |
| 463 | 402: ('Payment required', |
| 464 | 'No payment -- see charging schemes'), |
| 465 | 403: ('Forbidden', |
| 466 | 'Request forbidden -- authorization will not help'), |
| 467 | 404: ('Not found', 'Nothing matches the given URI'), |
| 468 | |
| 469 | 500: ('Internal error', 'Server got itself in trouble'), |
| 470 | 501: ('Not implemented', |
| 471 | 'Server does not support this operation'), |
| 472 | 502: ('Service temporarily overloaded', |
| 473 | 'The server cannot process the request due to a high load'), |
| 474 | 503: ('Gateway timeout', |
| 475 | 'The gateway server did not receive a timely response'), |
| 476 | |
| 477 | } |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 478 | |
| 479 | |
| 480 | def test(HandlerClass = BaseHTTPRequestHandler, |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 481 | ServerClass = HTTPServer): |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 482 | """Test the HTTP request handler class. |
| 483 | |
| 484 | This runs an HTTP server on port 8000 (or the first command line |
| 485 | argument). |
| 486 | |
| 487 | """ |
| 488 | |
| 489 | if sys.argv[1:]: |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 490 | port = string.atoi(sys.argv[1]) |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 491 | else: |
Guido van Rossum | 548703a | 1998-03-26 22:14:20 +0000 | [diff] [blame] | 492 | port = 8000 |
Guido van Rossum | 5c97167 | 1996-07-22 15:23:25 +0000 | [diff] [blame] | 493 | server_address = ('', port) |
| 494 | |
| 495 | httpd = ServerClass(server_address, HandlerClass) |
| 496 | |
| 497 | print "Serving HTTP on port", port, "..." |
| 498 | httpd.serve_forever() |
| 499 | |
| 500 | |
| 501 | if __name__ == '__main__': |
| 502 | test() |