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