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 if trans.get_content_type(): 18 content_type = trans.get_content_type().content_type 19 charset = trans.get_content_type().charset 20 else: 21 content_type, charset = None, None 22 out.write(""" 23 <html> 24 <head> 25 <title>Simple Example</title> 26 </head> 27 <body> 28 <h1>Test</h1> 29 <ul> 30 <li>Path: %s</li> 31 <li>Path without query: %s</li> 32 <li>Path info: %s</li> 33 <li>Query string: %s</li> 34 <li>Request method: %s</li> 35 <li>User: %s</li> 36 <li>Content type: %s</li> 37 <li>Charset: %s</li> 38 <li>Headers: 39 <dl> 40 %s 41 </dl> 42 </li> 43 <li>User-Agent: 44 <ul> 45 %s 46 </ul> 47 </li> 48 <li>user-agent: 49 <ul> 50 %s 51 </ul> 52 </li> 53 <li>Fields from path: 54 <ul> 55 %s 56 </ul> 57 </li> 58 <li>Fields from body: 59 <ul> 60 %s 61 </ul> 62 </li> 63 <li>Cookies: 64 <ul> 65 %s 66 </ul> 67 </li> 68 </ul> 69 </body> 70 </html> 71 """ % ( 72 trans.get_path(), 73 trans.get_path_without_query(), 74 trans.get_path_info(), 75 trans.get_query_string(), 76 trans.get_request_method(), 77 trans.get_user(), 78 content_type, 79 charset, 80 self._format_dict(trans.get_headers()), 81 self._format_list(trans.get_header_values("User-Agent")), 82 self._format_list(trans.get_header_values("user-agent")), 83 self._format_fields(trans.get_fields_from_path()), 84 self._format_fields(trans.get_fields_from_body()), 85 self._format_cookies(trans.get_cookies()), 86 )) 87 88 def _format_dict(self, d): 89 return "".join([ 90 "<dt>%s</dt><dd>%s</dd>" % (key, value) 91 for key, value in d.items() 92 ]) 93 94 def _format_fields(self, d): 95 return "".join([ 96 "<li>%s<ul>%s</ul></li>" % (key, self._format_list(value)) 97 for key, value in d.items() 98 ]) 99 100 def _format_cookies(self, d): 101 return "".join([ 102 "<dt>%s</dt><dd>%s</dd>" % (key, value.value) 103 for key, value in d.items() 104 ]) 105 106 def _format_list(self, l): 107 return "".join([ 108 "<li>%s</li>" % value 109 for value in l 110 ]) 111 112 # vim: tabstop=4 expandtab shiftwidth=4