
Download and unpack the tarballThis "quick start" explains how to install the
...Or clone the mercurial repository
Install WHIFF
Create a whiff "server root" directory
Add some files/applications to the directory
Run the directory in test mode and try it out
Install the directory as a CGI script and try it out
Where to next?
whiff package
and how to start building WHIFF applications.
The explanation proceeds by running console
commands and creating files using text editors.
The presentation assumes you have Python 2.5 or better
installed on your computer.
For reference, the directory containing the files
described in this quick start is provided in the WHIFF
distribution at INSTALL/doc/whiffTest.
If you would like to experiment with WHIFF without installing it please read the Test Drive Tutorial.
*.tar.gz tarball.
After you download the tarball, unpack it like this (or similar)
~$ gunzip ~/downloads/whiff.tar.gz
~$ tar -xvf whiff.tar
... creates directory ~/whiff
~$ hg clone https://whiff.googlecode.com/hg/ whiff
clone
doesn't work the archive may have moved: check the project
page for the new archive location.
hg clone
will get a development version of the package which may include
experimental enhancements.
Use the setup.py setup script to install the whiff package
(you may need superuser priviledges to execute this command).
~$ cd whiff
~/whiff$ python setup.py install
whiff package
as well as related scripts in the standard locations for
your Python location.
whiff is installed, try it
out by configuring a directory that whiff
can use to serve applications.
Make the directory whiffTest
for your whiff application and cd into
it so we can create some files in the new directory.
...$ cd ~
~$ mkdir whiffTest
~$ cd whiffTest
__init__.py initializer file which identifies
this directory as a directory for serving wsgi applications
and also publishes all templates in the directory and files with all
recognized mime extensions (for example *.jpg, *.html etcetera).
# contents of whiffTest/__init__.py
from whiff import resolver
# deliver wsgi application found in this directory!
__wsgi_directory__ = True
# publish templates and recognized mime extensions found in this directory!
resolver.publish_templates(__path__[0], globals(), mime_extensions=True,
directory_middleware=True)
__init__.py file tells WHIFF what to do with
this directory we can put some content here for WHIFF to serve up.
readme.txt file in this directory
so we can remember what the directory is for.
This is the content of readme.txt in the whiffTest directory:
just trying out WHIFF!
whiffTest
directory the server will automatically
recognize the readme.txt file and serve the contents of the
file to browsers as a plain text.
theTime.py
Create a Python file theTime.py which sends
a human readable time string for the current time from the server as HTML to a
client browser.
# contents of whiffTest/theTime.py
"""
Deliver the current time string as HTML.
"""
import time
def timeApplication(env, start_response):
result = time.ctime()
start_response("200 OK", [('Content-Type', 'text/html')])
return [result]
# identify timeApplication as the wsgi application for this module
__wsgi__ = timeApplication
### testing stuff...
def test():
from whiff.middleware import misc
env = {}
out = timeApplication(env, misc.ignore)
txt = "".join(list(out))
print repr(txt)
if __name__=="__main__":
test()
theTime.py
Python module source file has a "doc string" saying
what the module is for.
The next part of the file defines the logic of the application and the
last part provides a simple-minded self test (which just
makes sure the module runs and produces some output).
The assignment
__wsgi__ = timeApplication
timeApplication should handle all web requests
delegated to the theTime.py module.
theTime.py on the command linetheTime
module runs by executing the test interpretation.
~/whiffTest$ python theTime.py
'Fri Mar 27 16:15:32 2009'
hello.whiffhello
application which uses theTime
as an application fragment. The
hello application will
be defined using a WHIFF configuration template
instead of using a Python module.
Add a simple whiff configuration template hello.whiff
which displays some information about a
web request and shows the time.
{{comment:
This is the content of whiffTest/hello.whiff.
The following directive tells the server to deliver
the interpretation of this file as text/html.
/}}
{{env whiff.content_type: "text/html"/}}
<html>
<head>
<title>Hello there!</title>
</head>
<body>
This is your REMOTE_ADDRESS: {{get-env REMOTE_ADDR}}(UNKNOWN){{/get-env}}
<br>
The time is now: {{include "theTime"/}}
</body>
</html>
{{comment.. /}} is a whiff directive
containing configuration file comments which are ignored
by the configuration interpreter and stripped from the
configuration output.
{{env whiff.content_type: "text/html"/}}
is a configuration directive which modifies the application
environment, telling the WHIFF interpreter to serve the
hello application as HTML.
{{get-env REMOTE_ADDR}}(UNKNOWN){{/get-env}}
is a configuration
directive which evaluates to the value of the REMOTE_ADDR from
the web application environment.
{{include TheTime/}} is a configuration directive
which evaluates to the result of the theTime
application defined by Python code above.
Note that the relative locator for the application module theTime.py is
theTime with the .py extension stripped off.
hello.whiff parseshello.whiff
template contains no syntax errors using the whiff_parse.py script
that was installed when you installed the package in the set up.
~/whiffTest$ whiff_parse.py hello.whiff
parse succeeded
whiff_parse.py script
found a syntax error in the template
hello.whiff it would have been
reported in the output.
hello.whiff from the command linehello.whiff is a simple application which requires no
special environment entries we can run the template in "stand-alone" mode
from the command line using the whiff_expand.py
script installed with the package. Because the
hello template resides in the whiffTest directory
the testing script must run from the parent of whiffTest. Also
note that the locator for hello.whiff is hello with
the .whiff extension stripped off.
~/whiffTest$ cd ..
~$ whiff_expand.py whiffTest hello
<html>
<head>
<title>Hello there!</title>
</head>
<body>
This is your REMOTE_ADDRESS: 127.0.0.1
<br>
The time is now: Fri Mar 27 16:50:50 2009
</body>
</html>
hello.whiff as
interpreted in the directory whiffTest caused
run time errors
whiff_expand.py script.
Now we are ready to take a look at the contents of
the whiffTest directory in a browser.
~/whiffTest we start the script from the parent directory
~/ as follows:
~$ whiff_serve.py whiffTest/
serving whiff root <module 'whiffTest' from './whiffTest/__init__.pyc'>
as http on host localhost port 8888
_
localhost until the program is terminated in the terminal window
using a [CONTROL]-C. Until the server terminates the terminal window
will print brief log entries describing the pages served.
whiffTesthttp://localhost:8888/ to get
a web directory listing for whiffTest
whiffTest initialization
file __init__.py included the declaration
# publish templates and recognized mime extensions found in this directory!
resolver.publish_templates(__path__[0], globals(), mime_extensions=True,
directory_middleware=True)
directory_middleware=True enabled the default
directory listing for the directory.
directory_middleware=False instead.
The __init__ files are listed in the directory but the WHIFF server
will refuse to interpret these Python code files because they do not contain a
__wsgi__ entry indicating the application to use to handle web requests.
The server will interpret and serve content for the other files.
theTime.py from the directory listing we see the result
of evaluating the theTime application in the web browser.
hello applicationhello.whiff link we see the result of evaluating
the hello configuration template in the browser window.
readme.txt link from the directory listing will show the contents
of the readme file as plain text.
CONTROL-C or similar methods. On Windows you
may need to do a CONTROL-C and then send another request to the server
(by reloading the browser window) before the server will terminate.
whiffTest under an Apache server installation
as a simple CGI script.
/usr/local/apache2/cgi-bin/whiffTest.cgi
#!/python_install_location_path/python
import sys
import wsgiref.handlers
import whiff.resolver
sys.path.append("/whiffTest_parent_directory_path/")
import whiffTest
# create a WSGI application to serve the whiffTest directory
application = whiff.resolver.moduleRootApplication("/cgi-bin/whiffTest.cgi",
whiffTest)
# serve a CGI request using the directory
wsgiref.handlers.CGIHandler().run(application)
The application is defined by the assignment
application = whiff.resolver.moduleRootApplication("/cgi-bin/whiffTest.cgi",
whiffTest)
whiffTest Python module as a web directory
to respond to requests that start with the URL /cgi-bin/whiffTest.cgi.
As with all CGI scripts, the script source file must be marked executable before it will run.
/usr/local/apache2/cgi-bin$ chmod a+x whiffTest.cgi
The initial page show the default directory listing as before