1 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 2 <html xmlns="http://www.w3.org/1999/xhtml"> 3 <head> 4 <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" /> 5 <title>Applications and Resources</title> 6 <meta name="generator" 7 content="amaya 8.1a, see http://www.w3.org/Amaya/" /> 8 <link href="styles.css" rel="stylesheet" type="text/css" /> 9 </head> 10 <body> 11 <h1>Applications and Resources</h1> 12 At its simplest a WebStack application is just a single Python 13 class that we call a "resource". We can either create a module file <code>MyApplication.py</code> 14 (for example) or a 15 package directory <code>MyApplication</code> (for example) containing 16 a module file <code>__init__.py</code>, and we write into that file a 17 definition 18 of the resource as follows: 19 <pre>class MyResource:<br /> def respond(self, trans):<br /> [Examine the transaction, decide what the user wants to do.]<br /> [Perform some kind of action with the information supplied.]<br /> [Produce some kind of response which tells the user what happened.]</pre> 20 <p>It is in this kind of resource class that we write the 21 actual application code or at least the beginnings of it. When a user 22 of the application sends us a request, 23 the <code>respond</code> method will be called and the code 24 within it executed. The parts of the pseudo-code in 25 the above text which aren't valid Python 26 (ie. the bits in square brackets) will use the <code>trans</code> 27 object to find out what any given user of the application has sent us, 28 and to send information back 29 to the 30 user in response.</p> 31 <h2>Starting Simple</h2> 32 <p>The simplest way to turn this into a working application is to 33 ignore the first two activities mentioned in the pseudo-code and just 34 to produce some kind of 35 response. Here is how we might do that (in our module file <code>MyApplication.py</code> 36 or <code>MyApplication/__init__.py</code>):</p> 37 <pre>class MyResource:<br /> def respond(self, trans):<br /> out = trans.get_response_stream()<br /> print >>out, "Hello world."</pre> 38 <p>To deploy this, we need an adapter - see <a href="deploying.html">"Deploying 39 a WebStack Application"</a> for something which will work with this 40 example. 41 </p> 42 <h2>How Resources are Created 43 </h2> 44 <p>In the above example, the only thing we need to 45 consider is 46 what our code does, not how resource objects are created and invoked 47 (that is 48 done in the <a href="deploying.html">adapter code</a>). In more 49 complicated applications, there may 50 be a 51 need to create our own resource objects explicitly, but this is not 52 particularly interesting to think about at this point - see <a 53 href="paths-filesystem.html">"Treating the Path Like 54 a Filesystem"</a> for a 55 discussion of multiple resource objects.</p> 56 <h2>Design Considerations</h2> 57 <p>When writing an application, we 58 must consider a number of factors which 59 have an impact on the code (and other things) that we will need to 60 provide as 61 part of the finished product or service.</p> 62 <ul> 63 <li><a href="paths.html">URLs and Paths</a></li> 64 <li><a href="methods.html">Request Methods</a></li> 65 <li><a href="parameters.html">Request Parameters and Uploads</a></li> 66 <li><a href="responses.html">Responses and Presentation</a></li> 67 <li><a href="state.html">Cookies, Sessions and Persistent 68 Information</a></li> 69 </ul> 70 </body> 71 </html>