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"><head> 3 <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" /> 4 5 <title>Applications and Resources</title><meta name="generator" content="amaya 8.1a, see http://www.w3.org/Amaya/" /> 6 <link href="styles.css" rel="stylesheet" type="text/css" /></head> 7 8 <body> 9 <h1>Applications and Resources</h1> 10 At its simplest a WebStack application is just a single Python 11 class that we call a "resource". This class can be defined inside a 12 normal Python module or package, so let us start by doing the following:<br /> 13 <ol> 14 <li>Create a new directory for 15 our application; choose any name since we just want an empty space in 16 which to put new files.</li> 17 <li>Create a file called <code>MyApplication.py</code> 18 - this is our module.</li> 19 </ol> 20 We are going to call our resource <code>MyResource</code> 21 and in principle it will have a structure that looks like this: 22 <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> 23 <p>It is in this kind of resource 24 class that we write the 25 actual application code or at least the beginnings of it. When a user 26 of the application sends us a request, 27 the <code>respond</code> method 28 will be called and the code 29 within it executed. The parts of the pseudo-code in 30 the above text which aren't valid Python 31 (ie. the bits in square brackets) will, when we have written them, use 32 the <code>trans</code> 33 object to find out what any given user of the application has sent us, 34 and to send information back 35 to the 36 user in response.</p> 37 <h2>Starting Simple</h2> 38 <p>The simplest way to turn this 39 into a working application is to 40 ignore the first two activities mentioned in the pseudo-code and just 41 to produce some kind of 42 response. Here is how we can make our application do something:</p> 43 <ol> 44 <li>Edit the module 45 file <code>MyApplication.py</code>.</li> 46 <li>Write into it the following 47 code which defines <code>MyResource</code>:</li> 48 </ol> 49 <pre>class MyResource:<br /> def respond(self, trans):<br /> out = trans.get_response_stream()<br /> print >>out, "Hello world."</pre> 50 <h2>Testing the Resource</h2> 51 <p>To test this resource we need to deploy it, and to do that we need 52 an 53 adapter. Here is a quick way of writing an adapter and testing this 54 code:</p> 55 <ol> 56 <li> Create a file called <code>MyAdapter.py</code> - you 57 can choose another name if you want - this will be where the adapter 58 code lives.</li> 59 <li>Copy the example adapter in <a href="deploying.html">"Deploying 60 a WebStack Application"</a> and write it into <code>MyAdapter.py</code>.</li> 61 <li>Now, with two files in your directory, <code>MyApplication.py</code> 62 and <code>MyAdapter.py</code>, you may run <code>MyAdapter.py</code> 63 as follows:</li> 64 </ol> 65 <pre>python MyAdapter.py</pre> 66 <p>This should start the adapter program and print the following 67 message:</p> 68 <pre>Serving...</pre> 69 <p>You should now be able to visit <code>http://localhost:8080</code> 70 in your 71 browser and see the message printed by your application:</p> 72 <pre>Hello world.</pre></body></html>