WebStack

Annotated docs/resources.html

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