paulb@654 | 1 | <?xml version="1.0" encoding="iso-8859-1"?> |
paulb@348 | 2 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
paulb@453 | 3 | <html xmlns="http://www.w3.org/1999/xhtml"><head> |
paulb@348 | 4 | <meta content="text/html;charset=ISO-8859-1" http-equiv="Content-Type" /> |
paulb@654 | 5 | <title>Applications and Resources</title> |
paulb@453 | 6 | <link href="styles.css" rel="stylesheet" type="text/css" /></head> |
paulb@348 | 7 | <body> |
paulb@348 | 8 | <h1>Applications and Resources</h1> |
paulb@654 | 9 | At its simplest a WebStack application is just a single Python |
paulb@349 | 10 | class that we call a "resource". This class can be defined inside a |
paulb@349 | 11 | normal Python module or package, so let us start by doing the following:<br /> |
paulb@349 | 12 | <ol> |
paulb@349 | 13 | <li>Create a new directory for |
paulb@349 | 14 | our application; choose any name since we just want an empty space in |
paulb@349 | 15 | which to put new files.</li> |
paulb@349 | 16 | <li>Create a file called <code>MyApplication.py</code> |
paulb@349 | 17 | - this is our module.</li> |
paulb@349 | 18 | </ol> |
paulb@654 | 19 | We are going to call our resource <code>MyResource</code> |
paulb@349 | 20 | and in principle it will have a structure that looks like this: |
paulb@349 | 21 | <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> |
paulb@349 | 22 | <p>It is in this kind of resource |
paulb@349 | 23 | class that we write the |
paulb@348 | 24 | actual application code or at least the beginnings of it. When a user |
paulb@348 | 25 | of the application sends us a request, |
paulb@654 | 26 | the <code>respond</code> method |
paulb@349 | 27 | will be called and the code |
paulb@348 | 28 | within it executed. The parts of the pseudo-code in |
paulb@348 | 29 | the above text which aren't valid Python |
paulb@349 | 30 | (ie. the bits in square brackets) will, when we have written them, use |
paulb@654 | 31 | the <code>trans</code> |
paulb@348 | 32 | object to find out what any given user of the application has sent us, |
paulb@348 | 33 | and to send information back |
paulb@348 | 34 | to the |
paulb@348 | 35 | user in response.</p> |
paulb@348 | 36 | <h2>Starting Simple</h2> |
paulb@349 | 37 | <p>The simplest way to turn this |
paulb@349 | 38 | into a working application is to |
paulb@348 | 39 | ignore the first two activities mentioned in the pseudo-code and just |
paulb@348 | 40 | to produce some kind of |
paulb@349 | 41 | response. Here is how we can make our application do something:</p> |
paulb@349 | 42 | <ol> |
paulb@654 | 43 | <li>Edit the module |
paulb@654 | 44 | file <code>MyApplication.py</code>.</li> |
paulb@349 | 45 | <li>Write into it the following |
paulb@654 | 46 | code which defines <code>MyResource</code>:</li> |
paulb@349 | 47 | </ol> |
paulb@654 | 48 | <pre>class MyResource:<br /> def respond(self, trans):<br /> out = trans.get_response_stream()<br /> print >>out, "Hello world."</pre> |
paulb@349 | 49 | <h2>Testing the Resource</h2> |
paulb@349 | 50 | <p>To test this resource we need to deploy it, and to do that we need |
paulb@349 | 51 | an |
paulb@621 | 52 | adapter to connect it to the outside world. Here is a quick way of writing an adapter and testing this |
paulb@349 | 53 | code:</p> |
paulb@349 | 54 | <ol> |
paulb@654 | 55 | <li> Create a file called <code>MyAdapter.py</code> - you |
paulb@349 | 56 | can choose another name if you want - this will be where the adapter |
paulb@349 | 57 | code lives.</li> |
paulb@621 | 58 | <li>Write into it the following code:</li></ol><pre>from WebStack.Adapters.BaseHTTPRequestHandler import deploy # import the support for the server environment<br />from MyApplication import MyResource # import the main resource class<br />print "Serving..." # just for testing - we might want to remove this later<br />deploy(MyResource()) # connect a resource object to the server environment</pre><p>Now, with two files in your directory, <code>MyApplication.py</code> |
paulb@654 | 59 | and <code>MyAdapter.py</code>, you may run <code>MyAdapter.py</code> |
paulb@621 | 60 | as follows:</p><ol> |
paulb@621 | 61 | |
paulb@349 | 62 | </ol> |
paulb@349 | 63 | <pre>python MyAdapter.py</pre> |
paulb@349 | 64 | <p>This should start the adapter program and print the following |
paulb@349 | 65 | message:</p> |
paulb@349 | 66 | <pre>Serving...</pre> |
paulb@654 | 67 | <p>You should now be able to visit <code>http://localhost:8080</code> |
paulb@349 | 68 | in your |
paulb@654 | 69 | browser and see the message printed by your application:</p><pre>Hello world.</pre><h2>Related Examples</h2><p>The code presented in this document is very similar to that found in the following files:</p><ul><li><code>examples/Common/VerySimple/__init__.py</code> (where a package, <code>VerySimple</code>, is used to hold a <code>VerySimpleResource</code> class)</li><li><code>examples/BaseHTTPRequestHandler/VerySimpleApp.py</code> (where the resource is deployed)</li></ul><p>Note that a number of different adapters are provided in the <code>examples</code> directory hierarchy; for example:</p><ul><li><code>examples/CGI/VerySimpleHandler.py</code> (deploys the example as a CGI script)</li><li><code>examples/Twisted/VerySimpleApp.py</code> (deploys the example as a Twisted application)</li></ul><p>See <a href="deploying.html">"Deploying a WebStack Application"</a> for more information about adapters.</p></body></html> |