1 #!/usr/bin/env python 2 3 "A form submission test." 4 5 import WebStack.Generic 6 7 class FormResource: 8 9 """ 10 A resource which handles incoming form submissions. 11 """ 12 13 def respond(self, trans): 14 15 """ 16 Examine the incoming request, decoding the form information. 17 """ 18 19 # NOTE: Some frameworks do not pass in the content type. 20 21 content_type = trans.get_content_type() 22 input = trans.get_request_stream() 23 received_data = input.read() 24 25 # NOTE: Send the appropriate kind of response. 26 27 trans.set_content_type(WebStack.Generic.ContentType("text/html")) 28 out = trans.get_response_stream() 29 out.write(""" 30 <html> 31 <head> 32 <title>Form Test</title> 33 </head> 34 <body> 35 <h1>Form Test</h1> 36 <h2>Normal</h2> 37 <form method="post" action=""> 38 <input name="x" type="text" value="1"/><br/> 39 <input name="x" type="text" value="2"/><br/> 40 <input name="y" type="text" value="3"/><br/> 41 <input name="f" type="file"/><br/> 42 <input name="send" type="submit" value="Send!"/> 43 </form> 44 <h2>Multipart</h2> 45 <form method="post" action="" enctype="multipart/form-data"> 46 <input name="x" type="text" value="1"/><br/> 47 <input name="x" type="text" value="2"/><br/> 48 <input name="y" type="text" value="3"/><br/> 49 <input name="f" type="file"/><br/> 50 <input name="send" type="submit" value="Send!"/> 51 </form> 52 <h2>Content Type</h2> 53 <p>%s</p> 54 <h2>Received Data</h2> 55 <p>Start</p> 56 <pre>%s</pre> 57 <p>Stop</p> 58 </body> 59 </html> 60 """ % (content_type.content_type, received_data.replace("<", "<").replace(">", ">").replace("&", "&"))) 61 62 # vim: tabstop=4 expandtab shiftwidth=4