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