Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +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 |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 5 | (including CGI scripts). It does, however, optionally implement HTTP/1.1 |
| 6 | persistent connections, as of version 0.3. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 7 | |
| 8 | Contents: |
| 9 | |
| 10 | - BaseHTTPRequestHandler: HTTP request handler base class |
| 11 | - test: test function |
| 12 | |
| 13 | XXX To do: |
| 14 | |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 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 |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 18 | """ |
| 19 | |
| 20 | |
| 21 | # See also: |
| 22 | # |
| 23 | # HTTP Working Group T. Berners-Lee |
| 24 | # INTERNET-DRAFT R. T. Fielding |
| 25 | # <draft-ietf-http-v10-spec-00.txt> H. Frystyk Nielsen |
| 26 | # Expires September 8, 1995 March 8, 1995 |
| 27 | # |
| 28 | # URL: http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-v10-spec-00.txt |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 29 | # |
| 30 | # and |
| 31 | # |
| 32 | # Network Working Group R. Fielding |
| 33 | # Request for Comments: 2616 et al |
| 34 | # Obsoletes: 2068 June 1999 |
Tim Peters | 863ac44 | 2002-04-16 01:38:40 +0000 | [diff] [blame] | 35 | # Category: Standards Track |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 36 | # |
| 37 | # URL: http://www.faqs.org/rfcs/rfc2616.html |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 38 | |
| 39 | # Log files |
| 40 | # --------- |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 41 | # |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 42 | # Here's a quote from the NCSA httpd docs about log file format. |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 43 | # |
| 44 | # | The logfile format is as follows. Each line consists of: |
| 45 | # | |
| 46 | # | host rfc931 authuser [DD/Mon/YYYY:hh:mm:ss] "request" ddd bbbb |
| 47 | # | |
| 48 | # | host: Either the DNS name or the IP number of the remote client |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 49 | # | rfc931: Any information returned by identd for this person, |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 50 | # | - otherwise. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 51 | # | authuser: If user sent a userid for authentication, the user name, |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 52 | # | - otherwise. |
| 53 | # | DD: Day |
| 54 | # | Mon: Month (calendar name) |
| 55 | # | YYYY: Year |
| 56 | # | hh: hour (24-hour format, the machine's timezone) |
| 57 | # | mm: minutes |
| 58 | # | ss: seconds |
| 59 | # | request: The first line of the HTTP request as sent by the client. |
| 60 | # | ddd: the status code returned by the server, - if not available. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 61 | # | bbbb: the total number of bytes sent, |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 62 | # | *not including the HTTP/1.0 header*, - if not available |
| 63 | # | |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 64 | # | You can determine the name of the file accessed through request. |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 65 | # |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 66 | # (Actually, the latter is only true if you know the server configuration |
| 67 | # at the time the request was made!) |
| 68 | |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 69 | __version__ = "0.3" |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 70 | |
Skip Montanaro | e99d5ea | 2001-01-20 19:54:20 +0000 | [diff] [blame] | 71 | __all__ = ["HTTPServer", "BaseHTTPRequestHandler"] |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 72 | |
Jeremy Hylton | 7ffe299 | 2007-08-10 18:49:32 +0000 | [diff] [blame] | 73 | import io |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 74 | import sys |
| 75 | import time |
| 76 | import socket # For gethostbyaddr() |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 77 | import mimetools |
| 78 | import SocketServer |
| 79 | |
| 80 | # Default error message |
| 81 | DEFAULT_ERROR_MESSAGE = """\ |
| 82 | <head> |
| 83 | <title>Error response</title> |
| 84 | </head> |
| 85 | <body> |
| 86 | <h1>Error response</h1> |
| 87 | <p>Error code %(code)d. |
| 88 | <p>Message: %(message)s. |
| 89 | <p>Error code explanation: %(code)s = %(explain)s. |
| 90 | </body> |
| 91 | """ |
| 92 | |
Georg Brandl | a2aa1ac | 2005-06-26 21:33:14 +0000 | [diff] [blame] | 93 | def _quote_html(html): |
| 94 | return html.replace("&", "&").replace("<", "<").replace(">", ">") |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 95 | |
| 96 | class HTTPServer(SocketServer.TCPServer): |
| 97 | |
Guido van Rossum | 18865de | 2000-05-09 14:54:13 +0000 | [diff] [blame] | 98 | allow_reuse_address = 1 # Seems to make sense in testing environment |
| 99 | |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 100 | def server_bind(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 101 | """Override server_bind to store the server name.""" |
| 102 | SocketServer.TCPServer.server_bind(self) |
Martin v. Löwis | 3c120de | 2003-05-31 07:55:43 +0000 | [diff] [blame] | 103 | host, port = self.socket.getsockname()[:2] |
Peter Schneider-Kamp | 2d2785a | 2000-08-16 20:30:21 +0000 | [diff] [blame] | 104 | self.server_name = socket.getfqdn(host) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 105 | self.server_port = port |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 106 | |
| 107 | |
| 108 | class BaseHTTPRequestHandler(SocketServer.StreamRequestHandler): |
| 109 | |
| 110 | """HTTP request handler base class. |
| 111 | |
| 112 | The following explanation of HTTP serves to guide you through the |
| 113 | code as well as to expose any misunderstandings I may have about |
| 114 | HTTP (so you don't need to read the code to figure out I'm wrong |
| 115 | :-). |
| 116 | |
| 117 | HTTP (HyperText Transfer Protocol) is an extensible protocol on |
| 118 | top of a reliable stream transport (e.g. TCP/IP). The protocol |
| 119 | recognizes three parts to a request: |
| 120 | |
| 121 | 1. One line identifying the request type and path |
| 122 | 2. An optional set of RFC-822-style headers |
| 123 | 3. An optional data part |
| 124 | |
| 125 | The headers and data are separated by a blank line. |
| 126 | |
| 127 | The first line of the request has the form |
| 128 | |
| 129 | <command> <path> <version> |
| 130 | |
| 131 | where <command> is a (case-sensitive) keyword such as GET or POST, |
| 132 | <path> is a string containing path information for the request, |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 133 | and <version> should be the string "HTTP/1.0" or "HTTP/1.1". |
| 134 | <path> is encoded using the URL encoding scheme (using %xx to signify |
| 135 | the ASCII character with hex code xx). |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 136 | |
Andrew M. Kuchling | 8ca202e | 2003-02-03 15:21:15 +0000 | [diff] [blame] | 137 | The specification specifies that lines are separated by CRLF but |
| 138 | for compatibility with the widest range of clients recommends |
| 139 | servers also handle LF. Similarly, whitespace in the request line |
| 140 | is treated sensibly (allowing multiple spaces between components |
| 141 | and allowing trailing whitespace). |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 142 | |
| 143 | Similarly, for output, lines ought to be separated by CRLF pairs |
| 144 | but most clients grok LF characters just fine. |
| 145 | |
| 146 | If the first line of the request has the form |
| 147 | |
| 148 | <command> <path> |
| 149 | |
| 150 | (i.e. <version> is left out) then this is assumed to be an HTTP |
| 151 | 0.9 request; this form has no optional headers and data part and |
| 152 | the reply consists of just the data. |
| 153 | |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 154 | The reply form of the HTTP 1.x protocol again has three parts: |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 155 | |
| 156 | 1. One line giving the response code |
| 157 | 2. An optional set of RFC-822-style headers |
| 158 | 3. The data |
| 159 | |
| 160 | Again, the headers and data are separated by a blank line. |
| 161 | |
| 162 | The response code line has the form |
| 163 | |
| 164 | <version> <responsecode> <responsestring> |
| 165 | |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 166 | where <version> is the protocol version ("HTTP/1.0" or "HTTP/1.1"), |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 167 | <responsecode> is a 3-digit response code indicating success or |
| 168 | failure of the request, and <responsestring> is an optional |
| 169 | human-readable string explaining what the response code means. |
| 170 | |
| 171 | This server parses the request and the headers, and then calls a |
| 172 | function specific to the request type (<command>). Specifically, |
Guido van Rossum | ba895d8 | 1999-09-15 15:28:25 +0000 | [diff] [blame] | 173 | a request SPAM will be handled by a method do_SPAM(). If no |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 174 | such method exists the server sends an error response to the |
| 175 | client. If it exists, it is called with no arguments: |
| 176 | |
| 177 | do_SPAM() |
| 178 | |
| 179 | Note that the request name is case sensitive (i.e. SPAM and spam |
| 180 | are different requests). |
| 181 | |
| 182 | The various request details are stored in instance variables: |
| 183 | |
| 184 | - client_address is the client IP address in the form (host, |
| 185 | port); |
| 186 | |
| 187 | - command, path and version are the broken-down request line; |
| 188 | |
| 189 | - headers is an instance of mimetools.Message (or a derived |
| 190 | class) containing the header information; |
| 191 | |
| 192 | - rfile is a file object open for reading positioned at the |
| 193 | start of the optional input data part; |
| 194 | |
| 195 | - wfile is a file object open for writing. |
| 196 | |
| 197 | IT IS IMPORTANT TO ADHERE TO THE PROTOCOL FOR WRITING! |
| 198 | |
| 199 | The first thing to be written must be the response line. Then |
| 200 | follow 0 or more header lines, then a blank line, and then the |
| 201 | actual data (if any). The meaning of the header lines depends on |
| 202 | the command executed by the server; in most cases, when data is |
| 203 | returned, there should be at least one header line of the form |
| 204 | |
| 205 | Content-type: <type>/<subtype> |
| 206 | |
| 207 | where <type> and <subtype> should be registered MIME types, |
| 208 | e.g. "text/html" or "text/plain". |
| 209 | |
| 210 | """ |
| 211 | |
| 212 | # The Python system version, truncated to its first component. |
Eric S. Raymond | b49f4a4 | 2001-02-09 05:07:04 +0000 | [diff] [blame] | 213 | sys_version = "Python/" + sys.version.split()[0] |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 214 | |
| 215 | # The server software version. You may want to override this. |
| 216 | # The format is multiple whitespace-separated strings, |
| 217 | # where each string is of the form name[/version]. |
| 218 | server_version = "BaseHTTP/" + __version__ |
| 219 | |
Guido van Rossum | d65b539 | 1999-10-26 13:01:36 +0000 | [diff] [blame] | 220 | def parse_request(self): |
| 221 | """Parse a request (internal). |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 222 | |
Raymond Hettinger | bf68c78 | 2003-06-02 14:25:43 +0000 | [diff] [blame] | 223 | The request should be stored in self.raw_requestline; the results |
Guido van Rossum | d65b539 | 1999-10-26 13:01:36 +0000 | [diff] [blame] | 224 | are in self.command, self.path, self.request_version and |
| 225 | self.headers. |
| 226 | |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 227 | Return True for success, False for failure; on failure, an |
Guido van Rossum | d65b539 | 1999-10-26 13:01:36 +0000 | [diff] [blame] | 228 | error is sent back. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 229 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 230 | """ |
Andrew M. Kuchling | 2de97d3 | 2003-02-03 19:11:18 +0000 | [diff] [blame] | 231 | self.command = None # set in case of error on the first line |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 232 | self.request_version = version = "HTTP/0.9" # Default |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 233 | self.close_connection = 1 |
Jeremy Hylton | d2ef864 | 2007-08-03 20:32:27 +0000 | [diff] [blame] | 234 | requestline = str(self.raw_requestline, 'iso-8859-1') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 235 | if requestline[-2:] == '\r\n': |
| 236 | requestline = requestline[:-2] |
| 237 | elif requestline[-1:] == '\n': |
| 238 | requestline = requestline[:-1] |
| 239 | self.requestline = requestline |
Eric S. Raymond | b49f4a4 | 2001-02-09 05:07:04 +0000 | [diff] [blame] | 240 | words = requestline.split() |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 241 | if len(words) == 3: |
| 242 | [command, path, version] = words |
| 243 | if version[:5] != 'HTTP/': |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 244 | self.send_error(400, "Bad request version (%r)" % version) |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 245 | return False |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 246 | try: |
Andrew M. Kuchling | 2de97d3 | 2003-02-03 19:11:18 +0000 | [diff] [blame] | 247 | base_version_number = version.split('/', 1)[1] |
| 248 | version_number = base_version_number.split(".") |
| 249 | # RFC 2145 section 3.1 says there can be only one "." and |
| 250 | # - major and minor numbers MUST be treated as |
| 251 | # separate integers; |
| 252 | # - HTTP/2.4 is a lower version than HTTP/2.13, which in |
| 253 | # turn is lower than HTTP/12.3; |
| 254 | # - Leading zeros MUST be ignored by recipients. |
| 255 | if len(version_number) != 2: |
| 256 | raise ValueError |
| 257 | version_number = int(version_number[0]), int(version_number[1]) |
| 258 | except (ValueError, IndexError): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 259 | self.send_error(400, "Bad request version (%r)" % version) |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 260 | return False |
Andrew M. Kuchling | 2de97d3 | 2003-02-03 19:11:18 +0000 | [diff] [blame] | 261 | if version_number >= (1, 1) and self.protocol_version >= "HTTP/1.1": |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 262 | self.close_connection = 0 |
Andrew M. Kuchling | 2de97d3 | 2003-02-03 19:11:18 +0000 | [diff] [blame] | 263 | if version_number >= (2, 0): |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 264 | self.send_error(505, |
Andrew M. Kuchling | 2de97d3 | 2003-02-03 19:11:18 +0000 | [diff] [blame] | 265 | "Invalid HTTP Version (%s)" % base_version_number) |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 266 | return False |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 267 | elif len(words) == 2: |
| 268 | [command, path] = words |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 269 | self.close_connection = 1 |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 270 | if command != 'GET': |
| 271 | self.send_error(400, |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 272 | "Bad HTTP/0.9 request type (%r)" % command) |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 273 | return False |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 274 | elif not words: |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 275 | return False |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 276 | else: |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 277 | self.send_error(400, "Bad request syntax (%r)" % requestline) |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 278 | return False |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 279 | self.command, self.path, self.request_version = command, path, version |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 280 | |
Guido van Rossum | cfe02a4 | 2007-08-22 23:45:42 +0000 | [diff] [blame^] | 281 | # Examine the headers and look for a Connection directive. |
| 282 | |
| 283 | # MessageClass (rfc822) wants to see strings rather than bytes. |
| 284 | # But a TextIOWrapper around self.rfile would buffer too many bytes |
| 285 | # from the stream, bytes which we later need to read as bytes. |
| 286 | # So we read the correct bytes here, as bytes, then use StringIO |
| 287 | # to make them look like strings for MessageClass to parse. |
| 288 | headers = [] |
| 289 | while True: |
| 290 | line = self.rfile.readline() |
| 291 | headers.append(line) |
| 292 | if line in (b'\r\n', b'\n', b''): |
| 293 | break |
| 294 | hfile = io.StringIO(b''.join(headers).decode('iso-8859-1')) |
| 295 | self.headers = self.MessageClass(hfile) |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 296 | |
| 297 | conntype = self.headers.get('Connection', "") |
| 298 | if conntype.lower() == 'close': |
| 299 | self.close_connection = 1 |
| 300 | elif (conntype.lower() == 'keep-alive' and |
| 301 | self.protocol_version >= "HTTP/1.1"): |
| 302 | self.close_connection = 0 |
Tim Peters | bc0e910 | 2002-04-04 22:55:58 +0000 | [diff] [blame] | 303 | return True |
Guido van Rossum | d65b539 | 1999-10-26 13:01:36 +0000 | [diff] [blame] | 304 | |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 305 | def handle_one_request(self): |
Guido van Rossum | d65b539 | 1999-10-26 13:01:36 +0000 | [diff] [blame] | 306 | """Handle a single HTTP request. |
| 307 | |
| 308 | You normally don't need to override this method; see the class |
| 309 | __doc__ string for information on how to handle specific HTTP |
| 310 | commands such as GET and POST. |
| 311 | |
| 312 | """ |
Guido van Rossum | d65b539 | 1999-10-26 13:01:36 +0000 | [diff] [blame] | 313 | self.raw_requestline = self.rfile.readline() |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 314 | if not self.raw_requestline: |
| 315 | self.close_connection = 1 |
| 316 | return |
Guido van Rossum | d65b539 | 1999-10-26 13:01:36 +0000 | [diff] [blame] | 317 | if not self.parse_request(): # An error code has been sent, just exit |
| 318 | return |
| 319 | mname = 'do_' + self.command |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 320 | if not hasattr(self, mname): |
Walter Dörwald | 70a6b49 | 2004-02-12 17:35:32 +0000 | [diff] [blame] | 321 | self.send_error(501, "Unsupported method (%r)" % self.command) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 322 | return |
| 323 | method = getattr(self, mname) |
| 324 | method() |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 325 | |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 326 | def handle(self): |
| 327 | """Handle multiple requests if necessary.""" |
| 328 | self.close_connection = 1 |
| 329 | |
| 330 | self.handle_one_request() |
| 331 | while not self.close_connection: |
| 332 | self.handle_one_request() |
| 333 | |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 334 | def send_error(self, code, message=None): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 335 | """Send and log an error reply. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 336 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 337 | Arguments are the error code, and a detailed message. |
| 338 | The detailed message defaults to the short entry matching the |
| 339 | response code. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 340 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 341 | This sends an error response (so it must be called before any |
| 342 | output has been generated), logs the error, and finally sends |
| 343 | a piece of HTML explaining the error to the user. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 344 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 345 | """ |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 346 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 347 | try: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 348 | shortmsg, longmsg = self.responses[code] |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 349 | except KeyError: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 350 | shortmsg, longmsg = '???', '???' |
Raymond Hettinger | c041860 | 2002-05-31 23:03:33 +0000 | [diff] [blame] | 351 | if message is None: |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 352 | message = shortmsg |
| 353 | explain = longmsg |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 354 | self.log_error("code %d, message %s", code, message) |
Georg Brandl | a2aa1ac | 2005-06-26 21:33:14 +0000 | [diff] [blame] | 355 | # using _quote_html to prevent Cross Site Scripting attacks (see bug #1100201) |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 356 | content = (self.error_message_format % |
Georg Brandl | a2aa1ac | 2005-06-26 21:33:14 +0000 | [diff] [blame] | 357 | {'code': code, 'message': _quote_html(message), 'explain': explain}) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 358 | self.send_response(code, message) |
Skip Montanaro | 31fd86c | 2002-03-08 02:36:18 +0000 | [diff] [blame] | 359 | self.send_header("Content-Type", "text/html") |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 360 | self.send_header('Connection', 'close') |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 361 | self.end_headers() |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 362 | if self.command != 'HEAD' and code >= 200 and code not in (204, 304): |
| 363 | self.wfile.write(content) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 364 | |
| 365 | error_message_format = DEFAULT_ERROR_MESSAGE |
| 366 | |
| 367 | def send_response(self, code, message=None): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 368 | """Send the response header and log the response code. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 369 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 370 | Also send two standard headers with the server software |
| 371 | version and the current date. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 372 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 373 | """ |
| 374 | self.log_request(code) |
| 375 | if message is None: |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 376 | if code in self.responses: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 377 | message = self.responses[code][0] |
| 378 | else: |
| 379 | message = '' |
| 380 | if self.request_version != 'HTTP/0.9': |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 381 | self.wfile.write("%s %d %s\r\n" % |
| 382 | (self.protocol_version, code, message)) |
| 383 | # print (self.protocol_version, code, message) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 384 | self.send_header('Server', self.version_string()) |
| 385 | self.send_header('Date', self.date_time_string()) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 386 | |
| 387 | def send_header(self, keyword, value): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 388 | """Send a MIME header.""" |
| 389 | if self.request_version != 'HTTP/0.9': |
| 390 | self.wfile.write("%s: %s\r\n" % (keyword, value)) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 391 | |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 392 | if keyword.lower() == 'connection': |
| 393 | if value.lower() == 'close': |
| 394 | self.close_connection = 1 |
| 395 | elif value.lower() == 'keep-alive': |
| 396 | self.close_connection = 0 |
| 397 | |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 398 | def end_headers(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 399 | """Send the blank line ending the MIME headers.""" |
| 400 | if self.request_version != 'HTTP/0.9': |
| 401 | self.wfile.write("\r\n") |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 402 | |
| 403 | def log_request(self, code='-', size='-'): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 404 | """Log an accepted request. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 405 | |
Andrew M. Kuchling | ec73cd4 | 2006-03-07 16:16:07 +0000 | [diff] [blame] | 406 | This is called by send_response(). |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 407 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 408 | """ |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 409 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 410 | self.log_message('"%s" %s %s', |
| 411 | self.requestline, str(code), str(size)) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 412 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 413 | def log_error(self, format, *args): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 414 | """Log an error. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 415 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 416 | This is called when a request cannot be fulfilled. By |
| 417 | default it passes the message on to log_message(). |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 418 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 419 | Arguments are the same as for log_message(). |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 420 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 421 | XXX This should go to the separate error log. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 422 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 423 | """ |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 424 | |
Thomas Wouters | fc7bb8c | 2007-01-15 15:49:28 +0000 | [diff] [blame] | 425 | self.log_message(format, *args) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 426 | |
| 427 | def log_message(self, format, *args): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 428 | """Log an arbitrary message. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 429 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 430 | This is used by all other logging functions. Override |
| 431 | it if you have specific logging wishes. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 432 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 433 | The first argument, FORMAT, is a format string for the |
| 434 | message to be logged. If the format string contains |
| 435 | any % escapes requiring parameters, they should be |
| 436 | specified as subsequent arguments (it's just like |
| 437 | printf!). |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 438 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 439 | The client host and current date/time are prefixed to |
| 440 | every message. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 441 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 442 | """ |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 443 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 444 | sys.stderr.write("%s - - [%s] %s\n" % |
| 445 | (self.address_string(), |
| 446 | self.log_date_time_string(), |
| 447 | format%args)) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 448 | |
| 449 | def version_string(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 450 | """Return the server software version string.""" |
| 451 | return self.server_version + ' ' + self.sys_version |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 452 | |
Georg Brandl | 5d07696 | 2006-02-17 13:34:16 +0000 | [diff] [blame] | 453 | def date_time_string(self, timestamp=None): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 454 | """Return the current date and time formatted for a message header.""" |
Georg Brandl | 5d07696 | 2006-02-17 13:34:16 +0000 | [diff] [blame] | 455 | if timestamp is None: |
| 456 | timestamp = time.time() |
| 457 | year, month, day, hh, mm, ss, wd, y, z = time.gmtime(timestamp) |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 458 | s = "%s, %02d %3s %4d %02d:%02d:%02d GMT" % ( |
| 459 | self.weekdayname[wd], |
| 460 | day, self.monthname[month], year, |
| 461 | hh, mm, ss) |
| 462 | return s |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 463 | |
| 464 | def log_date_time_string(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 465 | """Return the current time formatted for logging.""" |
| 466 | now = time.time() |
| 467 | year, month, day, hh, mm, ss, x, y, z = time.localtime(now) |
| 468 | s = "%02d/%3s/%04d %02d:%02d:%02d" % ( |
| 469 | day, self.monthname[month], year, hh, mm, ss) |
| 470 | return s |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 471 | |
| 472 | weekdayname = ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun'] |
| 473 | |
| 474 | monthname = [None, |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 475 | 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', |
| 476 | 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'] |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 477 | |
| 478 | def address_string(self): |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 479 | """Return the client address formatted for logging. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 480 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 481 | This version looks up the full hostname using gethostbyaddr(), |
| 482 | and tries to find a name that contains at least one dot. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 483 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 484 | """ |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 485 | |
Martin v. Löwis | 3c120de | 2003-05-31 07:55:43 +0000 | [diff] [blame] | 486 | host, port = self.client_address[:2] |
Peter Schneider-Kamp | 2d2785a | 2000-08-16 20:30:21 +0000 | [diff] [blame] | 487 | return socket.getfqdn(host) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 488 | |
| 489 | # Essentially static class variables |
| 490 | |
| 491 | # The version of the HTTP protocol we support. |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 492 | # Set this to HTTP/1.1 to enable automatic keepalive |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 493 | protocol_version = "HTTP/1.0" |
| 494 | |
| 495 | # The Message-like class used to parse headers |
| 496 | MessageClass = mimetools.Message |
| 497 | |
| 498 | # Table mapping response codes to messages; entries have the |
| 499 | # form {code: (shortmessage, longmessage)}. |
Georg Brandl | 6aab16e | 2006-02-17 19:17:25 +0000 | [diff] [blame] | 500 | # See RFC 2616. |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 501 | responses = { |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 502 | 100: ('Continue', 'Request received, please continue'), |
| 503 | 101: ('Switching Protocols', |
| 504 | 'Switching to new protocol; obey Upgrade header'), |
| 505 | |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 506 | 200: ('OK', 'Request fulfilled, document follows'), |
| 507 | 201: ('Created', 'Document created, URL follows'), |
| 508 | 202: ('Accepted', |
| 509 | 'Request accepted, processing continues off-line'), |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 510 | 203: ('Non-Authoritative Information', 'Request fulfilled from cache'), |
Georg Brandl | 6aab16e | 2006-02-17 19:17:25 +0000 | [diff] [blame] | 511 | 204: ('No Content', 'Request fulfilled, nothing follows'), |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 512 | 205: ('Reset Content', 'Clear input form for further input.'), |
| 513 | 206: ('Partial Content', 'Partial content follows.'), |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 514 | |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 515 | 300: ('Multiple Choices', |
| 516 | 'Object has several resources -- see URI list'), |
| 517 | 301: ('Moved Permanently', 'Object moved permanently -- see URI list'), |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 518 | 302: ('Found', 'Object moved temporarily -- see URI list'), |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 519 | 303: ('See Other', 'Object moved -- see Method and URL list'), |
Georg Brandl | 6aab16e | 2006-02-17 19:17:25 +0000 | [diff] [blame] | 520 | 304: ('Not Modified', |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 521 | 'Document has not changed since given time'), |
| 522 | 305: ('Use Proxy', |
| 523 | 'You must use proxy specified in Location to access this ' |
| 524 | 'resource.'), |
| 525 | 307: ('Temporary Redirect', |
| 526 | 'Object moved temporarily -- see URI list'), |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 527 | |
Georg Brandl | 6aab16e | 2006-02-17 19:17:25 +0000 | [diff] [blame] | 528 | 400: ('Bad Request', |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 529 | 'Bad request syntax or unsupported method'), |
| 530 | 401: ('Unauthorized', |
| 531 | 'No permission -- see authorization schemes'), |
Georg Brandl | 6aab16e | 2006-02-17 19:17:25 +0000 | [diff] [blame] | 532 | 402: ('Payment Required', |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 533 | 'No payment -- see charging schemes'), |
| 534 | 403: ('Forbidden', |
| 535 | 'Request forbidden -- authorization will not help'), |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 536 | 404: ('Not Found', 'Nothing matches the given URI'), |
| 537 | 405: ('Method Not Allowed', |
| 538 | 'Specified method is invalid for this server.'), |
| 539 | 406: ('Not Acceptable', 'URI not available in preferred format.'), |
| 540 | 407: ('Proxy Authentication Required', 'You must authenticate with ' |
| 541 | 'this proxy before proceeding.'), |
Georg Brandl | 6aab16e | 2006-02-17 19:17:25 +0000 | [diff] [blame] | 542 | 408: ('Request Timeout', 'Request timed out; try again later.'), |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 543 | 409: ('Conflict', 'Request conflict.'), |
| 544 | 410: ('Gone', |
| 545 | 'URI no longer exists and has been permanently removed.'), |
| 546 | 411: ('Length Required', 'Client must specify Content-Length.'), |
| 547 | 412: ('Precondition Failed', 'Precondition in headers is false.'), |
| 548 | 413: ('Request Entity Too Large', 'Entity is too large.'), |
| 549 | 414: ('Request-URI Too Long', 'URI is too long.'), |
| 550 | 415: ('Unsupported Media Type', 'Entity body in unsupported format.'), |
| 551 | 416: ('Requested Range Not Satisfiable', |
| 552 | 'Cannot satisfy request range.'), |
| 553 | 417: ('Expectation Failed', |
| 554 | 'Expect condition could not be satisfied.'), |
Tim Peters | 11cf605 | 2001-01-14 21:54:20 +0000 | [diff] [blame] | 555 | |
Georg Brandl | 6aab16e | 2006-02-17 19:17:25 +0000 | [diff] [blame] | 556 | 500: ('Internal Server Error', 'Server got itself in trouble'), |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 557 | 501: ('Not Implemented', |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 558 | 'Server does not support this operation'), |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 559 | 502: ('Bad Gateway', 'Invalid responses from another server/proxy.'), |
Georg Brandl | 6aab16e | 2006-02-17 19:17:25 +0000 | [diff] [blame] | 560 | 503: ('Service Unavailable', |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 561 | 'The server cannot process the request due to a high load'), |
Georg Brandl | 6aab16e | 2006-02-17 19:17:25 +0000 | [diff] [blame] | 562 | 504: ('Gateway Timeout', |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 563 | 'The gateway server did not receive a timely response'), |
Georg Brandl | 6aab16e | 2006-02-17 19:17:25 +0000 | [diff] [blame] | 564 | 505: ('HTTP Version Not Supported', 'Cannot fulfill request.'), |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 565 | } |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 566 | |
| 567 | |
| 568 | def test(HandlerClass = BaseHTTPRequestHandler, |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 569 | ServerClass = HTTPServer, protocol="HTTP/1.0"): |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 570 | """Test the HTTP request handler class. |
| 571 | |
| 572 | This runs an HTTP server on port 8000 (or the first command line |
| 573 | argument). |
| 574 | |
| 575 | """ |
| 576 | |
| 577 | if sys.argv[1:]: |
Eric S. Raymond | 5ff63d6 | 2001-02-09 05:38:46 +0000 | [diff] [blame] | 578 | port = int(sys.argv[1]) |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 579 | else: |
Guido van Rossum | 45e2fbc | 1998-03-26 21:13:24 +0000 | [diff] [blame] | 580 | port = 8000 |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 581 | server_address = ('', port) |
| 582 | |
Martin v. Löwis | 587c98c | 2002-03-17 18:37:22 +0000 | [diff] [blame] | 583 | HandlerClass.protocol_version = protocol |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 584 | httpd = ServerClass(server_address, HandlerClass) |
| 585 | |
Martin v. Löwis | a43c2f8 | 2001-07-24 20:34:08 +0000 | [diff] [blame] | 586 | sa = httpd.socket.getsockname() |
Guido van Rossum | be19ed7 | 2007-02-09 05:37:30 +0000 | [diff] [blame] | 587 | print("Serving HTTP on", sa[0], "port", sa[1], "...") |
Guido van Rossum | e7e578f | 1995-08-04 04:00:20 +0000 | [diff] [blame] | 588 | httpd.serve_forever() |
| 589 | |
| 590 | |
| 591 | if __name__ == '__main__': |
| 592 | test() |