1 #!/usr/bin/env python 2 3 "A simple application for test purposes." 4 5 import WebStack.Generic 6 7 class SimpleResource: 8 9 "A simple resource." 10 11 def respond(self, trans): 12 trans.set_content_type(WebStack.Generic.ContentType("text/html")) 13 14 # Get some information. 15 16 out = trans.get_response_stream() 17 out.write(""" 18 <html> 19 <head> 20 <title>Simple Example</title> 21 </head> 22 <body> 23 <h1>Test</h1> 24 <ul> 25 <li>Path: %s</li> 26 <li>Path info: %s</li> 27 <li>Query string: %s</li> 28 <li>Request method: %s</li> 29 <li>Content type: %s</li> 30 <li>Charset: %s</li> 31 <li>Headers: 32 <dl> 33 %s 34 </dl> 35 </li> 36 <li>User-Agent: 37 <ul> 38 %s 39 </ul> 40 </li> 41 <li>user-agent: 42 <ul> 43 %s 44 </ul> 45 </li> 46 <li>Fields from path: 47 <ul> 48 %s 49 </ul> 50 </li> 51 <li>Fields from body: 52 <ul> 53 %s 54 </ul> 55 </li> 56 <li>Cookies: 57 <ul> 58 %s 59 </ul> 60 </li> 61 </ul> 62 </body> 63 </html> 64 """ % ( 65 trans.get_path(), 66 trans.get_path_info(), 67 trans.get_query_string(), 68 trans.get_request_method(), 69 trans.get_content_type().content_type, 70 trans.get_content_type().charset, 71 self._format_dict(trans.get_headers()), 72 self._format_list(trans.get_header_values("User-Agent")), 73 self._format_list(trans.get_header_values("user-agent")), 74 self._format_dict(trans.get_fields_from_path()), 75 self._format_dict(trans.get_fields_from_body()), 76 self._format_dict(trans.get_cookies()), 77 )) 78 79 def _format_dict(self, d): 80 return "".join([ 81 "<dt>%s</dt><dd>%s</dd>" % (key, value) 82 for key, value in d.items() 83 ]) 84 85 def _format_list(self, l): 86 return "".join([ 87 "<li>%s</li>" % value 88 for value in l 89 ]) 90 91 # vim: tabstop=4 expandtab shiftwidth=4