]>
code.delx.au - gnu-emacs/blob - test/cedet/tests/test.py
1 # Test file for Python language.
3 # $Id: test.py,v 1.2 2009/09/28 23:23:31 cyd Exp $
5 # Simle class compount statement with blank lines sprinkled.
12 # Simple def statement with no argument
16 # Simple def statement with arguments
28 a
=b
=c
=d
=e
=f
=i
=j
=k
=l
=m
=n
=o
=p
=q
=r
=s
=t
=x
=y
=1
51 # implicit continuation lines, see
52 # http://docs.python.org/ref/implicit-joining.html
62 a_hash
= { 'a':1, "b":2,
76 class longclasslist(xx
.yyy
,
81 # wisent-python.wy chokes on this! -ryk 6/17/02
83 class HTTPServer(xxx
.yyy
):
84 allow_reuse_address
= 1 # Seems to make sense in testing environment
85 def server_bind(self
):
86 SocketServer
.TCPServer
.server_bind(self
)
87 host
, port
= self
.socket
.getsockname()
88 self
.server_name
= socket
.getfqdn(host
)
89 self
.server_port
= port
92 #########################################################################
93 ### /usr/lib/python2.2/BaseHTTPServer.py
94 #########################################################################
96 """HTTP server base class.
98 Note: the class in this module doesn't implement any HTTP request; see
99 SimpleHTTPServer for simple implementations of GET, HEAD and POST
100 (including CGI scripts).
104 - BaseHTTPRequestHandler: HTTP request handler base class
105 - test: test function
109 - send server version
110 - log requests even later (to capture byte count)
111 - log user-agent header and other interesting goodies
112 - send error log to separate file
113 - are request names really case sensitive?
120 # HTTP Working Group T. Berners-Lee
121 # INTERNET-DRAFT R. T. Fielding
122 # <draft-ietf-http-v10-spec-00.txt> H. Frystyk Nielsen
123 # Expires September 8, 1995 March 8, 1995
125 # URL: http://www.ics.uci.edu/pub/ietf/http/draft-ietf-http-v10-spec-00.txt
131 # Here's a quote from the NCSA httpd docs about log file format.
133 # | The logfile format is as follows. Each line consists of:
135 # | host rfc931 authuser [DD/Mon/YYYY:hh:mm:ss] "request" ddd bbbb
137 # | host: Either the DNS name or the IP number of the remote client
138 # | rfc931: Any information returned by identd for this person,
140 # | authuser: If user sent a userid for authentication, the user name,
143 # | Mon: Month (calendar name)
145 # | hh: hour (24-hour format, the machine's timezone)
148 # | request: The first line of the HTTP request as sent by the client.
149 # | ddd: the status code returned by the server, - if not available.
150 # | bbbb: the total number of bytes sent,
151 # | *not including the HTTP/1.0 header*, - if not available
153 # | You can determine the name of the file accessed through request.
155 # (Actually, the latter is only true if you know the server configuration
156 # at the time the request was made!)
161 __all__
= ["HTTPServer", "BaseHTTPRequestHandler"]
165 import socket
# For gethostbyaddr()
169 # Default error message
170 DEFAULT_ERROR_MESSAGE
= """\
172 <title>Error response</title>
175 <h1>Error response</h1>
176 <p>Error code %(code)d.
177 <p>Message: %(message)s.
178 <p>Error code explanation: %(code)s = %(explain)s.
183 class HTTPServer(SocketServer
.TCPServer
):
185 allow_reuse_address
= 1 # Seems to make sense in testing environment
187 def server_bind(self
):
188 """Override server_bind to store the server name."""
189 SocketServer
.TCPServer
.server_bind(self
)
190 host
, port
= self
.socket
.getsockname()
191 self
.server_name
= socket
.getfqdn(host
)
192 self
.server_port
= port
195 class BaseHTTPRequestHandler(SocketServer
.StreamRequestHandler
):
197 """HTTP request handler base class.
199 The following explanation of HTTP serves to guide you through the
200 code as well as to expose any misunderstandings I may have about
201 HTTP (so you don't need to read the code to figure out I'm wrong
204 HTTP (HyperText Transfer Protocol) is an extensible protocol on
205 top of a reliable stream transport (e.g. TCP/IP). The protocol
206 recognizes three parts to a request:
208 1. One line identifying the request type and path
209 2. An optional set of RFC-822-style headers
210 3. An optional data part
212 The headers and data are separated by a blank line.
214 The first line of the request has the form
216 <command> <path> <version>
218 where <command> is a (case-sensitive) keyword such as GET or POST,
219 <path> is a string containing path information for the request,
220 and <version> should be the string "HTTP/1.0". <path> is encoded
221 using the URL encoding scheme (using %xx to signify the ASCII
222 character with hex code xx).
224 The protocol is vague about whether lines are separated by LF
225 characters or by CRLF pairs -- for compatibility with the widest
226 range of clients, both should be accepted. Similarly, whitespace
227 in the request line should be treated sensibly (allowing multiple
228 spaces between components and allowing trailing whitespace).
230 Similarly, for output, lines ought to be separated by CRLF pairs
231 but most clients grok LF characters just fine.
233 If the first line of the request has the form
237 (i.e. <version> is left out) then this is assumed to be an HTTP
238 0.9 request; this form has no optional headers and data part and
239 the reply consists of just the data.
241 The reply form of the HTTP 1.0 protocol again has three parts:
243 1. One line giving the response code
244 2. An optional set of RFC-822-style headers
247 Again, the headers and data are separated by a blank line.
249 The response code line has the form
251 <version> <responsecode> <responsestring>
253 where <version> is the protocol version (always "HTTP/1.0"),
254 <responsecode> is a 3-digit response code indicating success or
255 failure of the request, and <responsestring> is an optional
256 human-readable string explaining what the response code means.
258 This server parses the request and the headers, and then calls a
259 function specific to the request type (<command>). Specifically,
260 a request SPAM will be handled by a method do_SPAM(). If no
261 such method exists the server sends an error response to the
262 client. If it exists, it is called with no arguments:
266 Note that the request name is case sensitive (i.e. SPAM and spam
267 are different requests).
269 The various request details are stored in instance variables:
271 - client_address is the client IP address in the form (host,
274 - command, path and version are the broken-down request line;
276 - headers is an instance of mimetools.Message (or a derived
277 class) containing the header information;
279 - rfile is a file object open for reading positioned at the
280 start of the optional input data part;
282 - wfile is a file object open for writing.
284 IT IS IMPORTANT TO ADHERE TO THE PROTOCOL FOR WRITING!
286 The first thing to be written must be the response line. Then
287 follow 0 or more header lines, then a blank line, and then the
288 actual data (if any). The meaning of the header lines depends on
289 the command executed by the server; in most cases, when data is
290 returned, there should be at least one header line of the form
292 Content-type: <type>/<subtype>
294 where <type> and <subtype> should be registered MIME types,
295 e.g. "text/html" or "text/plain".
299 # The Python system version, truncated to its first component.
300 sys_version
= "Python/" + sys
.version
.split()[0]
302 # The server software version. You may want to override this.
303 # The format is multiple whitespace-separated strings,
304 # where each string is of the form name[/version].
305 server_version
= "BaseHTTP/" + __version__
307 def parse_request(self
):
308 """Parse a request (internal).
310 The request should be stored in self.raw_request; the results
311 are in self.command, self.path, self.request_version and
314 Return value is 1 for success, 0 for failure; on failure, an
318 self
.request_version
= version
= "HTTP/0.9" # Default
319 requestline
= self
.raw_requestline
320 if requestline
[-2:] == '\r\n':
321 requestline
= requestline
[:-2]
322 elif requestline
[-1:] == '\n':
323 requestline
= requestline
[:-1]
324 self
.requestline
= requestline
325 words
= requestline
.split()
327 [command
, path
, version
] = words
328 if version
[:5] != 'HTTP/':
329 self
.send_error(400, "Bad request version (%s)" % `version`
)
331 elif len(words
) == 2:
332 [command
, path
] = words
335 "Bad HTTP/0.9 request type (%s)" % `command`
)
338 self
.send_error(400, "Bad request syntax (%s)" % `requestline`
)
340 self
.command
, self
.path
, self
.request_version
= command
, path
, version
341 self
.headers
= self
.MessageClass(self
.rfile
, 0)
345 """Handle a single HTTP request.
347 You normally don't need to override this method; see the class
348 __doc__ string for information on how to handle specific HTTP
349 commands such as GET and POST.
353 self
.raw_requestline
= self
.rfile
.readline()
354 if not self
.parse_request(): # An error code has been sent, just exit
356 mname
= 'do_' + self
.command
357 if not hasattr(self
, mname
):
358 self
.send_error(501, "Unsupported method (%s)" % `self
.command`
)
360 method
= getattr(self
, mname
)
363 def send_error(self
, code
, message
=None):
364 """Send and log an error reply.
366 Arguments are the error code, and a detailed message.
367 The detailed message defaults to the short entry matching the
370 This sends an error response (so it must be called before any
371 output has been generated), logs the error, and finally sends
372 a piece of HTML explaining the error to the user.
377 short
, long = self
.responses
[code
]
379 short
, long = '???', '???'
383 self
.log_error("code %d, message %s", code
, message
)
384 self
.send_response(code
, message
)
385 self
.send_header("Content-Type", "text/html")
387 self
.wfile
.write(self
.error_message_format
%
392 error_message_format
= DEFAULT_ERROR_MESSAGE
394 def send_response(self
, code
, message
=None):
395 """Send the response header and log the response code.
397 Also send two standard headers with the server software
398 version and the current date.
401 self
.log_request(code
)
403 if self
.responses
.has_key(code
):
404 message
= self
.responses
[code
][0]
407 if self
.request_version
!= 'HTTP/0.9':
408 self
.wfile
.write("%s %s %s\r\n" %
409 (self
.protocol_version
, str(code
), message
))
410 self
.send_header('Server', self
.version_string())
411 self
.send_header('Date', self
.date_time_string())
413 def send_header(self
, keyword
, value
):
414 """Send a MIME header."""
415 if self
.request_version
!= 'HTTP/0.9':
416 self
.wfile
.write("%s: %s\r\n" % (keyword
, value
))
418 def end_headers(self
):
419 """Send the blank line ending the MIME headers."""
420 if self
.request_version
!= 'HTTP/0.9':
421 self
.wfile
.write("\r\n")
423 def log_request(self
, code
='-', size
='-'):
424 """Log an accepted request.
426 This is called by send_reponse().
430 self
.log_message('"%s" %s %s',
431 self
.requestline
, str(code
), str(size
))
433 def log_error(self
, *args
):
436 This is called when a request cannot be fulfilled. By
437 default it passes the message on to log_message().
439 Arguments are the same as for log_message().
441 XXX This should go to the separate error log.
445 apply(self
.log_message
, args
)
447 def log_message(self
, format
, *args
):
448 """Log an arbitrary message.
450 This is used by all other logging functions. Override
451 it if you have specific logging wishes.
453 The first argument, FORMAT, is a format string for the
454 message to be logged. If the format string contains
455 any % escapes requiring parameters, they should be
456 specified as subsequent arguments (it's just like
459 The client host and current date/time are prefixed to
464 sys
.stderr
.write("%s - - [%s] %s\n" %
465 (self
.address_string(),
466 self
.log_date_time_string(),
469 def version_string(self
):
470 """Return the server software version string."""
471 return self
.server_version
+ ' ' + self
.sys_version
473 def date_time_string(self
):
474 """Return the current date and time formatted for a message header."""
476 year
, month
, day
, hh
, mm
, ss
, wd
, y
, z
= time
.gmtime(now
)
477 s
= "%s, %02d %3s %4d %02d:%02d:%02d GMT" % (
478 self
.weekdayname
[wd
],
479 day
, self
.monthname
[month
], year
,
483 def log_date_time_string(self
):
484 """Return the current time formatted for logging."""
486 year
, month
, day
, hh
, mm
, ss
, x
, y
, z
= time
.localtime(now
)
487 s
= "%02d/%3s/%04d %02d:%02d:%02d" % (
488 day
, self
.monthname
[month
], year
, hh
, mm
, ss
)
491 weekdayname
= ['Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat', 'Sun']
494 'Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun',
495 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec']
497 def address_string(self
):
498 """Return the client address formatted for logging.
500 This version looks up the full hostname using gethostbyaddr(),
501 and tries to find a name that contains at least one dot.
505 host
, port
= self
.client_address
506 return socket
.getfqdn(host
)
508 # Essentially static class variables
510 # The version of the HTTP protocol we support.
511 # Don't override unless you know what you're doing (hint: incoming
512 # requests are required to have exactly this version string).
513 protocol_version
= "HTTP/1.0"
515 # The Message-like class used to parse headers
516 MessageClass
= mimetools
.Message
518 # Table mapping response codes to messages; entries have the
519 # form {code: (shortmessage, longmessage)}.
520 # See http://www.w3.org/hypertext/WWW/Protocols/HTTP/HTRESP.html
522 200: ('OK', 'Request fulfilled, document follows'),
523 201: ('Created', 'Document created, URL follows'),
525 'Request accepted, processing continues off-line'),
526 203: ('Partial information', 'Request fulfilled from cache'),
527 204: ('No response', 'Request fulfilled, nothing follows'),
529 301: ('Moved', 'Object moved permanently -- see URI list'),
530 302: ('Found', 'Object moved temporarily -- see URI list'),
531 303: ('Method', 'Object moved -- see Method and URL list'),
532 304: ('Not modified',
533 'Document has not changed singe given time'),
536 'Bad request syntax or unsupported method'),
537 401: ('Unauthorized',
538 'No permission -- see authorization schemes'),
539 402: ('Payment required',
540 'No payment -- see charging schemes'),
542 'Request forbidden -- authorization will not help'),
543 404: ('Not found', 'Nothing matches the given URI'),
545 500: ('Internal error', 'Server got itself in trouble'),
546 501: ('Not implemented',
547 'Server does not support this operation'),
548 502: ('Service temporarily overloaded',
549 'The server cannot process the request due to a high load'),
550 503: ('Gateway timeout',
551 'The gateway server did not receive a timely response'),
556 def test(HandlerClass
= BaseHTTPRequestHandler
,
557 ServerClass
= HTTPServer
):
558 """Test the HTTP request handler class.
560 This runs an HTTP server on port 8000 (or the first command line
566 port
= int(sys
.argv
[1])
569 server_address
= ('', port
)
571 httpd
= ServerClass(server_address
, HandlerClass
)
573 sa
= httpd
.socket
.getsockname()
574 print "Serving HTTP on", sa
[0], "port", sa
[1], "..."
575 httpd
.serve_forever()
578 if __name__
== '__main__':
581 # arch-tag: 567449b3-cc90-45b6-bbe3-1e113995bdae